C# “元素”是无效的 XmlNodeType
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/660811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Element' is an invalid XmlNodeType
提问by user72603
I don't get this. I really don't get the ReadEndElement. I assume after every ReadStartElement, you need to close off the reader to advanced to the next start element and if there's no more start elements, close by ReadEndElement for all other elements?
我不明白这个。我真的没有得到 ReadEndElement。我假设在每个 ReadStartElement 之后,您需要关闭阅读器以前进到下一个开始元素,如果没有更多的开始元素,则通过 ReadEndElement 关闭所有其他元素?
Sample of the returned XML:
返回的 XML 示例:
<Envelope>
<Body>
<RESULT>
<SUCCESS>true</SUCCESS>
<SESSIONID>dc302149861088513512481</SESSIONID>
<ENCODING>dc302149861088513512481
</ENCODING>
</RESULT>
</Body>
</Envelope>
reader.Read();
reader.ReadStartElement("Envelope");
reader.ReadStartElement("Body");
reader.ReadStartElement("RESULT");
reader.ReadStartElement("SUCCESS");
reader.ReadEndElement();
reader.ReadStartElement("SESSIONID");
_sessionID = reader.ReadString();
reader.ReadEndElement();
reader.ReadEndElement(); <-- error here
reader.ReadEndElement();
reader.ReadEndElement();
I am ignoring one of the elements (ENCODING) retuned, because I don't need it...not sure if that has anything to do with it. Maybe I'm required to read every element regardless if I want to use it or not.
我忽略了重新调整的元素(编码)之一,因为我不需要它......不确定这是否与它有关。也许我需要阅读每个元素,不管我是否想使用它。
采纳答案by dtb
You have to read every node (attribute, element, ...) in the document.
您必须阅读文档中的每个节点(属性、元素等)。
If the reader is positioned on an element, you may skip it (and all its subnodes) with XmlReader.Skip.
如果阅读器位于元素上,您可以使用XmlReader.Skip跳过它(及其所有子节点)。
回答by CountZero
I experienced this error when attempting to deserialize using the XmlSerializer. I have a valid XML which contained some <br />
HTML tags which were un-encoded in my source document.
尝试使用 XmlSerializer 进行反序列化时遇到此错误。我有一个有效的 XML,其中包含一些<br />
未在我的源文档中编码的 HTML 标签。
<Event>
<Id>1</Id>
<Title>The first Sicknote with Chris Liberator</Title>
<Copy>
Sicknote - Techno vs Dubstep <br />
------------------------------------<br />
<br /><br />
Thursday 8th October - 11pm - 4am
</Copy>
<Date>2009-10-08T10:00:00</Date>
<FlierImage>1.jpg</FlierImage>
</Event>