我正在开发一个使用 java web 服务的 .net 应用程序,我通过 wsdl.exe 工具创建了客户端,它工作正常,这是 web 服务的方法之一,接收一个 xml 文档作为参数,我使用XmlTextWriter
生成文档,但是在 xml 文档中包含特殊字符时遇到问题,例如:
我生成的文档如下所示:
<xml-parameter>
<some-field> this is text whit a (>) charatcer</some-field>
</xml-parameter>
使用 fiddler 检查生成的请求,我看到它的开头是这样转义的:
<xml-parameter>
<some-field> this is a text whit a (&gt;) character $lt;/some-field>
<xml-parameter>
我可以看到它被转义为 " & gt; " 而不是> 我已经准备好尝试使用实体而不是实际角色,但没有运气,提前感谢 =)。
编辑:这里的代码用于生成 xml,基本上我使用内存流和 xmltextwriter 生成 xml,然后读取整个流,我将生成的 xml 传递给我的服务代理。
string query = "/AGS_Polizas/INBOUNDLINK/@SOURCEITEMREF = > * ";
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.WriteStartDocument(true);
writer.WriteStartElement("RunQueryRequest");
writer.WriteAttributeString("xmlns", "http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema");
writer.WriteAttributeString("maxResults", "0");
writer.WriteAttributeString("version", "latest-version(.)");
writer.WriteAttributeString("contentOption", "URL");
writer.WriteAttributeString("retrieveOption", "ITEMTREE");
writer.WriteStartElement("AuthenticationData");
writer.WriteAttributeString("connectString", "SCHEMA=ICMADMIN");
writer.WriteAttributeString("configString", "");
writer.WriteStartElement("ServerDef");
writer.WriteStartElement("ServerType");
writer.WriteString("ICM");
writer.WriteEndElement();
writer.WriteStartElement("ServerName");
writer.WriteString("icmnlsdb");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("LoginData");
writer.WriteStartElement("UserID");
writer.WriteString("icmadmin");
writer.WriteEndElement();
writer.WriteStartElement("Password");
writer.WriteString("Passw0rd");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("QueryCriteria");
writer.WriteStartElement("QueryString");
writer.WriteString(query);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
string xml = reader.ReadToEnd();
writer.Close();
reader.Close();