在 XML SOAP 请求 body 中添加 XML 字符串作为值时出现 XmlException
XmlException when adding an XML string as a value in an XML SOAP request body
我正在编写一个利用 XML SOAP 的应用程序。不幸的是,编写请求的代码不在我的手中,因为它是一个只提供给我的 DLL 文件。下载Jetbrain的dotPeek反编译DLL文件,看代码,发现soap message requestbody是这样写的
string message = "<Value>" + myValue + "</Value>";
然后解析为 XElement。
现在这在大多数情况下都有效,因为大多数请求都接受正常值(例如 <Value>true</Value>
)。但是我们正在研究的 xml 技术中有一个特定的协议,其中我们必须将整个 xml 字符串作为值传递,例如
string myValue = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Foobar ID=\"123\" />";
这会引发错误:
Unexpected XML declaration. The XML declaration must be the first node
in the document, and no white space characters are allowed to appear
before it.
我的结果 xml body 看起来像:
<Value><?xml version="1.0" encoding="utf-8"?><Foobar ID="123" /></Value>
我可以通过某种方式传递这个 xml 字符串同时避免错误吗?我正在用 C# 编写所有这些。感谢您的帮助。
几个小时后终于找到了答案。最快的解决方案是将开始和结束标记转换为 character entities
string myValue = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Foobar ID=\"123\" />";
myValue = myValue.Replace("<", "<").Replace(">", ">");
您也可以选择使用
- HttpUtility.HtmlEncode
- System.Net.WebUtility.HtmlEncode
我正在编写一个利用 XML SOAP 的应用程序。不幸的是,编写请求的代码不在我的手中,因为它是一个只提供给我的 DLL 文件。下载Jetbrain的dotPeek反编译DLL文件,看代码,发现soap message requestbody是这样写的
string message = "<Value>" + myValue + "</Value>";
然后解析为 XElement。
现在这在大多数情况下都有效,因为大多数请求都接受正常值(例如 <Value>true</Value>
)。但是我们正在研究的 xml 技术中有一个特定的协议,其中我们必须将整个 xml 字符串作为值传递,例如
string myValue = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Foobar ID=\"123\" />";
这会引发错误:
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.
我的结果 xml body 看起来像:
<Value><?xml version="1.0" encoding="utf-8"?><Foobar ID="123" /></Value>
我可以通过某种方式传递这个 xml 字符串同时避免错误吗?我正在用 C# 编写所有这些。感谢您的帮助。
几个小时后终于找到了答案。最快的解决方案是将开始和结束标记转换为 character entities
string myValue = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Foobar ID=\"123\" />";
myValue = myValue.Replace("<", "<").Replace(">", ">");
您也可以选择使用
- HttpUtility.HtmlEncode
- System.Net.WebUtility.HtmlEncode