代码之家  ›  专栏  ›  技术社区  ›  luke

XmlReader读取带有unescaped的文档(&s)

  •  0
  • luke  · 技术社区  · 14 年前

    我正在尝试将从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);
    }
    

    但我有个例外:

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

    我查看了该字符周围的文档,它位于一个随机标记的中间,但是在20-30个字符之前,我注意到有未替换的符号(&所以我认为这就是问题所在。

    跑步:

    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'
    

    1 回复  |  直到 14 年前
        1
  •  1
  •   µBio    14 年前

    验证您的xml编码是否与他们的匹配(文档顶部,类似于 <?xml version="1.0" encoding="ISO-8859-9"?> ). 将webservice xml文档中的值替换为 webserviceEncoding 在......下面

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

    1. 在将其加载到xml解析器之前,在字符串中替换它
    推荐文章