C# 处理 SOAP 响应时出现 XML 解析错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/381403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:21:09  来源:igfitidea点击:

XML Parse error while processing the SOAP response

c#vb.netweb-services

提问by rptony

I am using C# .Net 2.0 to write a webservices client. The server's soap implementation is tested and is pretty solid. gSoap/C++ applications had no problem reading the responses. However the .Net implementation complains "There is an error in XML document" while calling one of the methods. Similar responses recieved from the server were happily processed by the xml parser.

我正在使用 C# .Net 2.0 编写一个 webservices 客户端。服务器的soap 实现已经过测试并且非常可靠。gSoap/C++ 应用程序读取响应没有问题。但是,.Net 实现在调用其中一种方法时会抱怨“XML 文档中存在错误”。xml 解析器很高兴地处理了从服务器收到的类似响应。

Looks like to me the MSXML parser (I hope thats the one .Net is been using) is a very unforgiving parser.

在我看来,MSXML 解析器(我希望这是 .Net 一直在使用的那个)是一个非常无情的解析器。

I have no control over the server. Some how I have to work around this problem. So, I was thinking of writing a SoapExtension as describe here

我无法控制服务器。一些我必须如何解决这个问题。所以,我想写一个 SoapExtension 描述在这里

So my question is, can I hook a parser before Deserialize stage and completely bypass the Deserialize stage.

所以我的问题是,我可以在反序列化阶段之前挂钩解析器并完全绕过反序列化阶段。

And above all, how do i instruct the SOAP stub to use my extended class ?

最重要的是,我如何指示 SOAP 存根使用我的扩展类?

采纳答案by Kev

First of all I'd grab a snapshot of the failing XML in the Deserialise stage to try and diagnose the problem.

首先,我会在反序列化阶段获取失败的 XML 的快照,以尝试诊断问题。

You can hook your soap extension into your client app without recompiling. Just add:

您可以将您的soap 扩展挂接到您的客户端应用程序中,而无需重新编译。只需添加:

<webServices>
    <soapExtensionTypes>
        <add type="DebugTools.SOAP.SOAPTrace.SoapTraceExtension, DebugTools.SOAP" 
               priority="0" group="High"/>
    </soapExtensionTypes>
</webServices>

DebugTools.SOAP.SOAPTraceis the namespace of the SoapTraceExtension
DebugTools.SOAPis the name of the assembly containing the soap trace code.

DebugTools.SOAP.SOAPTraceSoapTraceExtension的命名空间
DebugTools.SOAP是包含 soap 跟踪代码的程序集的名称。

to your app.config or web.config file.

到您的 app.config 或 web.config 文件。

It would be handy if you could paste in the complete exception with stack trace. There may be something really obvious. Also, if possible the WSDL for the web service, that would be veryuseful.

如果您可以使用堆栈跟踪粘贴完整的异常,那将会很方便。可能有一些非常明显的事情。此外,如果可能的话,Web 服务的 WSDL 将非常有用。

回答by Kribensa

If you would like to step through the serialise/deserialise code, you can use a tool called SGen. This comes with VS in the SDK, and is used as follows:

如果您想单步执行序列化/反序列化代码,可以使用名为 SGen 的工具。这是SDK中VS自带的,用法如下:

  1. Compile the app using the normal VS-generated (or wsdl.exe generated) proxy classes (These are usually hidden and are in a file called Reference.cs

  2. Drop to the Visual Studio cmd prompt, and open the Debug/Release folder (i.e. the folder the exe has been compiled to)

  3. Type the following at the cmd prompt (replacing TheApplicationName with yor app's name): SGEN /keep TheApplicationName.exe

  1. 使用普通 VS 生成(或 wsdl.exe 生成)代理类编译应用程序(这些通常是隐藏的,位于名为 Reference.cs 的文件中

  2. 拖到 Visual Studio cmd 提示符,打开 Debug/Release 文件夹(即 exe 已编译到的文件夹)

  3. 在 cmd 提示符下键入以下内容(将 TheApplicationName 替换为您的应用程序名称):SGEN /keep TheApplicationName.exe

You will now see a number of files have been created. Delete all the generated files except for the .cs file (including the dll it creates)

您现在将看到已经创建了许多文件。删除除 .cs 文件之外的所有生成的文件(包括它创建的 dll)

Move the .cs file to the source folder, and include it in your project.

将 .cs 文件移动到源文件夹,并将其包含在您的项目中。

Add the following attribute to your Reference.cs :

将以下属性添加到您的 Reference.cs :

[XmlSerializerAssembly()]

You can now step through the actual serialising and deserialising code to find the actual problem (and if there's no other way to fix the problem, you can alter this generated code as required)

您现在可以逐步执行实际的序列化和反序列化代码以找到实际问题(如果没有其他方法可以解决问题,您可以根据需要更改此生成的代码)

回答by John Saunders

BTW, .NET does not use MSXML. It has its own implementation. The performance would be horrible if XmlReader were calling out to MSXML for every Readcall.

顺便说一句,.NET 不使用 MSXML。它有自己的实现。如果 XmlReader 每次读取调用都调用 MSXML,那么性能会很糟糕。