简体   繁体   English

从有效的XML获取内容后,“解析EntityName时发生错误”

[英]“An error occurred while parsing EntityName” after grabbing content from valid XML

I am reading an XML string with XDocument 我正在用XDocument读取XML字符串

XmlReader reader = XmlReader.Create(new StringReader(xmltext));
        reader.Read();
        XDocument xdoc = XDocument.Load(reader);

Then I grab the content of some tags and put them within tags in a different string. 然后,我抓取一些标签的内容,并将它们放在标签中的其他字符串中。 When I try to Load this string in the same way I did with the first, I get an error "An error occurred while parsing EntityName. Line 1, position 344.". 当我尝试以与第一个相同的方式加载此字符串时,收到错误消息“解析EntityName。第1行,位置344时发生错误。”。

I think it should be parsed correctly since it has beem parsed before so I guess I am missing something here. 我认为应该正确解析它,因为之前已经对其进行了解析,所以我想我在这里遗漏了一些东西。

I am reading and copying the content of the first XML with (string)i.Element("field") . 我正在使用(string)i.Element("field")阅读和复制第一个XML的内容。

I am using .net 4 我正在使用.net 4

When I grab the content of the xml that I want to use for building another Xml string I use (string)i.Element("field") and this is converting my Xml into string. 当我获取要用于构建另一个Xml字符串的xml的内容时,我使用(string)i.Element("field") ,这(string)i.Element("field")我的Xml转换为字符串。 My next Xml Parsing does not recognize it as an Element anymore so I solved the problem by not using (string) before I read my element, just i.Element("field") and this works. 我的下一个Xml Parsing不再将其识别为元素,因此我在读取元素之前不使用(string)解决了问题,而仅使用i.Element("field")

It sounds like you've got something like this: 听起来您有这样的事情:

<OriginalDocument>
    <Foo>A &amp; B</Foo>
</OriginalDocument>

That A &amp; B 那个A &amp; B A &amp; B represents the text A & B . A &amp; B代表文本A & B So when you grab the text from the element, you'll get the string "A & B". 因此,当您从元素中获取文本时,将获得字符串“ A&B”。 If you then use that to build a new element like this: 如果您随后使用它来构建一个像这样的新元素:

string foo = "<Foo>" + fooText + "</Foo>";

then you'll end up with invalid XML like this: 那么您最终将获得无效的XML,如下所示:

<Foo>A & B</Foo>

Basically, you shouldn't be constructing XML in text form. 基本上,您不应该以文本形式构造XML。 It's not clear what you're really trying to achieve, but you can copy an element from one place to another pretty easily in XElement form; 目前尚不清楚您真正想要实现的目标,但是您可以轻松地以XElement形式将一个元素从一个位置复制到另一个位置。 you shouldn't need to build a string and then reparse it. 您不需要构建字符串然后重新解析它。

So after spending hours on this issue: it turns out that if you have an ampersand symbol ("&") or any other XML escape characters within your xml string, it will always fail will you try read the XML. 因此,在花了几个小时解决这个问题之后:事实证明,如果您在xml字符串中包含一个&符号或其他XML转义字符,则在尝试读取XML时它将总是失败。 TO solve this, replace the special characters with their escaped string format 要解决此问题,请将特殊字符替换为转义的字符串格式

 YourXmlString = YourXmlString.Replace("'", "&apos;").Replace("\"", "&quot;").Replace(">", "&gt;").Replace("<", "&lt;").Replace("&", "&amp;");

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

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