0

我很难弄清楚这一点,我有一个 XML 文档,它有多个同名节点。在这些节点中甚至还有更多具有相同名称但属性不同的节点,这就是我想要捕获的内容。以下是 XML 的示例:

<?xml version="1.0" encoding="utf-8"?>
<TopologyDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/online/managementsystems/topologydefinition/2009/11">
    <Topology Name="testenv">
        <DataCenters>
            <DataCenter Name="FL" Type="Active">
                <Machines>
                    <Machine Name="FLVMServer1" VmHost="FLVHost100">
                        <IPBindings>
                            <IPBinding VirtualNetworkType="Data" IP="192.168.0.10" />
                        </IPBindings>
                    </Machine>
                        <Machine Name="FLVMServer2" VmHost="FLVHost200">
                        <IPBindings>
                            <IPBinding VirtualNetworkType="Data" IP="192.168.0.20" />
                        </IPBindings>
                    </Machine>
            </DataCenter>
            <DataCenter Name="RI" Type="Passive">
                <Machines>
                    <Machine Name="RIVMServer1" VmHost="RIVHost100">
                        <IPBindings>
                            <IPBinding VirtualNetworkType="Data" IP="192.168.2.10" />
                        </IPBindings>
                    </Machine>
                        <Machine Name="RIVMServer2" VmHost="RIVHost200">
                        <IPBindings>
                            <IPBinding VirtualNetworkType="Data" IP="192.168.2.20" />
                        </IPBindings>
                    </Machine>
            </DataCenter>
        </DataCenters>
    </Topology>
</TopologyDefinition>

我需要为所有 DC 捕获以下内容:机器名称 VmHost IP

我已经尝试过 XPATH,我也尝试过遍历每个节点,但没有运气

        ServerInfoClass serverInfo = new ServerInfoClass();
        XmlDocument doc = new XmlDocument();
        doc.Load(FilePath);

        XmlNodeList dcElemList = doc.GetElementsByTagName("DataCenter");
        for(int j = 0; j < dcElemList.Count; j++)
        {
            XmlNodeList elemList = doc.GetElementsByTagName("Machine");
            for (int i = 0; i < elemList.Count; i++)
            {
                serverInfo.ServerName = elemList[i].Attributes["Name"].Value;
                serverInfo.VmHost = elemList[i].Attributes["VmHost"].Value;

                XmlNodeList ipList = doc.GetElementsByTagName("IPBindings");
                for (int x = 0; x < ipList.Count; x++) ;
                {
                    //serverInfo.IPAddress = ipList[x].Attributes["IP"].Value;
                }
                OutPut(serverInfo.ServerName, serverInfo.VmHost, serverInfo.IPAddress);
            }
        }
4

2 回答 2

1

如果你把它放到一个 XDocument 中,你可以像这样查询它:

XDocument document = // ... your document.
var ns = document.Root.Name.Namespace;
var results = from dcNode in document.Descendants(ns + "DataCenter")
    let Name = dcNode.Attribute("Name").Value
    let Type = dcNode.Attribute("Type").Value
    let Machines = dcNode.Descendants(ns + "Machine").Select(mNode => 
        new { 
            Name = mNode.Attribute("Name").Value,
            VmHost = mNode.Attribute("VmHost").Value,
            Bindings = mNode.Descendants(ns + "IPBinding").Attributes("IP").Select(x => x.Value).ToArray()
        })
    select new { Name, Type, Machines };

我建议制作一个辅助扩展方法来获取属性值,它还检查空值。

这里的关键之一是记住您的 XML 元素存在于命名空间中,并且您需要在查询时提供完整的命名空间。这就是为什么首先ns从根节点提取命名空间实例很方便的原因。

于 2012-12-18T21:54:08.367 回答
1

您的示例格式不正确。您缺少</Machines>标签。

另一种方法是使用 LINQ to XML:

XNamespace ns = "http://schemas.microsoft.com/online/managementsystems/topologydefinition/2009/11";
foreach (var machine in XElement.Load(@"c:\mydata.xml").Descendants(ns + "Machine"))
{
    string name = machine.Attribute("Name").Value;
    string vmHost = machine.Attribute("VmHost").Value;

    XElement ipBinding = machine.Descendants(ns + "IPBinding").Single();
    string vnType = ipBinding.Attribute("VirtualNetworkType").Value;
    string ip = ipBinding.Attribute("IP").Value;
}
于 2012-12-18T23:55:33.020 回答