简体   繁体   English

XmlReader使用未转义的&s读取文档

[英]XmlReader read document with unescaped &s

I am trying to parse an XMl document that i received into a string from a web service call. 我正在尝试将我从Web服务调用中收到的XMl文档解析为字符串。

String content = ...;//long xml document
using(TextReader reader = new StringReader(content))
using(XmlReader xml_reader = XmlReader.Create(reader, settings))
{
    XML = new XPathDocument(xml_reader);
}

however i get an exception : 但是我得到一个例外:

An error occurred while parsing EntityName. Line 1, position 1721.

i looked through the document around that character and it was in the middle of a random tag, however about 20-30 chars earlier i noticed that there were unescaped ampersands (& characters), so im thinking that that is the problem. 我浏览了该字符周围的文档,它位于一个随机标记的中间,但是大约20到30个字符,我注意到其中存在未转义的&符号(&字符),因此我认为这就是问题所在。

running: 运行:

content.Substring(1700, 100);//results in the following text
"alue>1 time per day& with^honey~&water\\\\</Value></Frequency></Direction>          </Directions>     "
                    ^unescaped & char 1721 is the 'w'

how can i successful read this document as xml? 我如何成功以xml格式读取此文档?

verify that your xml encoding matches theirs (the top of the document, something like <?xml version="1.0" encoding="ISO-8859-9"?> ). 验证您的xml编码是否与他们的xml编码匹配(文档顶部,例如<?xml version="1.0" encoding="ISO-8859-9"?> )。 Substitute the value from the webservice xml document for webserviceEncoding below 将webservice xml文档中的值替换为下面的webserviceEncoding

using(XmlReader r = XmlReader.Create(new StreamReader(fileName, Encoding.GetEncoding(webserviceEncoding)))) {
    XML = new XPathDocument( r );
    // ... 
}

If that doesn't work 如果那不起作用

  1. Replace it in the string prior to loading it into an xml parser 在将其加载到xml解析器中之前,将其替换为字符串
  2. Notify the webservice vendor 通知网络服务供应商

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM