简体   繁体   English

无法将XmlReader加载到XDocument中

[英]Can't load XmlReader into XDocument

I'm trying to load an XmlReader into an XDocument for easier manipulation. 我正在尝试将XmlReaderXDocument以便于操作。 The XML is well formed and valid (I double checked). XML格式正确且有效(我仔细检查过)。 When I try and load it into the XDocument , I get an InvalidOperationException 当我尝试将其加载到XDocument ,我得到一个InvalidOperationException

The XmlReader state should be EndOfFile after this operation. 此操作后,XmlReader状态应为EndOfFile。

the code to load this is 加载它的代码是

public void ReadXml(System.Xml.XmlReader reader)
{
    var doc = XDocument.Load(reader);
}

I've included a sample of the XML that causes the problem. 我已经包含了导致问题的XML示例。 I can serialize and deserialize this class without a problem, but not load it. 我可以顺利地序列化和反序列化这个类,但不加载它。 Any ideas? 有任何想法吗?

<?xml version="1.0" encoding="utf-8"?>
<ForestView xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Forest>
 <TreeNodeView>
  <Level>Master</Level>
  <ID>39476b1f-e2f8-4d76-b82e-a5166899ad43</ID>
  <Name>Black Mesa</Name>
  <ServerIPAddress>127.0.0.1</ServerIPAddress>
  <ServerPortNumber>8000</ServerPortNumber>
  <ClientIPAddress>NA</ClientIPAddress>
  <ClientPortNumber>4000</ClientPortNumber>
  <Nodes>
    <Level>Server</Level>
    <NodeID>062c3e03-235d-4d7d-9b60-c6228c9cc89e</NodeID>
    <Name />
    <ServerIPAddress>127.0.0.1</ServerIPAddress>
    <ServerPortNumber>5000</ServerPortNumber>
    <ClientIPAddress>127.0.0.1</ClientIPAddress>
    <ClientPortNumber>4000</ClientPortNumber>
  </Nodes>
  <Nodes>
    <Level>Intermediate</Level>
    <NodeID>9bafdc9e-771e-42cf-8f03-e7e75a67a6d1</NodeID>
    <Name>Jen</Name>
    <ServerIPAddress>127.0.0.1</ServerIPAddress>
    <ServerPortNumber>8001</ServerPortNumber>
    <ClientIPAddress>127.0.0.1</ClientIPAddress>
    <ClientPortNumber>8000</ClientPortNumber>
    <Nodes>
      <Level>Terminal</Level>
      <NodeID>72509141-0ab8-45c1-8042-30afb233b4a8</NodeID>
      <Name>Mary</Name>
      <ServerIPAddress>127.0.0.1</ServerIPAddress>
      <ServerPortNumber>0</ServerPortNumber>
      <ClientIPAddress>127.0.0.1</ClientIPAddress>
      <ClientPortNumber>8001</ClientPortNumber>
    </Nodes>
   </Nodes>
  </TreeNodeView>
 </Forest>
</ForestView>

Quite late answer, in case somebody is having the same issue. 很晚才回答,以防有人遇到同样的问题。 You can fix it by using ReadSubtree() on the reader, like: 您可以通过在阅读器上使用ReadSubtree()来修复它,例如:

public void ReadXml(System.Xml.XmlReader reader)
{
     var doc = XDocument.Load(reader.ReadSubtree());
}

This error implies that there is extra data after the reader has loaded what it thinks is an XML document. 此错误意味着在读者加载了它认为是XML文档的内容之后会有额外的数据。 Please verify that your XML file contains no data after the final closing element and that the XmlReader is initialized to read from the root element or start of the file, and nothing else. 请确认您的XML文件在最终关闭元素之后不包含任何数据,并且初始化XmlReader以从根元素或文件的开头读取,而不是其他内容。

Another late answer, but I ran into a problem with the reader.ReadSubtree() workaround and this ended up working for my situation. 另一个迟到的答案,但我遇到了reader.ReadSubtree()解决方法的问题,这最终适用于我的情况。 The basic approach is reading into a string, then calling XDocument.Parse() : 基本方法是读入一个字符串,然后调用XDocument.Parse()

XDocument doc;
var textReader = reader as XmlTextReader;
if (textReader != null)
{
    var xml = textReader.ReadOuterXml();
    doc = XDocument.Parse(xml);
}
else
    doc = XDocument.Load(reader); 

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

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