gpt4 book ai didi

c# - 无法将 XmlReader 加载到 XDocument 中

转载 作者:可可西里 更新时间:2023-11-01 03:13:03 27 4
gpt4 key购买 nike

我正在尝试将 XmlReader 加载到 XDocument 中以便于操作。 XML 格式正确且有效(我仔细检查过)。当我尝试将它加载到 XDocument 时,我得到一个 InvalidOperationException

The XmlReader state should be EndOfFile after this operation.

加载这个的代码是

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

我提供了导致问题的 XML 示例。我可以毫无问题地序列化和反序列化此类,但不能加载它。有什么想法吗?

<?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>

最佳答案

很晚的回答,以防有人遇到同样的问题。您可以通过在阅读器上使用 ReadSubtree() 来修复它,例如:

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

关于c# - 无法将 XmlReader 加载到 XDocument 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1133643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com