检索多个节点的元素属性

时间:2012-12-18 21:42:30

标签: c# xml-parsing

我正在努力解决这个问题,我有一个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);
            }
        }

2 个答案:

答案 0 :(得分: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命名空间实例很方便。

答案 1 :(得分: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;
}