如何读取具有多个名称空间且多个子节点具有相同名称的 xml soap 响应

how to read an xml soap response with multiple namespaces and with same name for multiple child nodes

我们向 cisco cucm 发出了 soap 请求并得到了这个结果。 我们需要读取此结果并获取每个设备(项目节点)的详细信息。 问题是有多个节点同名 "item" 最好的阅读方式是什么?

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:selectCmDeviceResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://schemas.cisco.com/ast/soap">
            <SelectCmDeviceResult xsi:type="ns2:SelectCmDeviceResult" xmlns:ns2="http://ccm.cisco.com/serviceability/soap/risport70/">
                <TotalDevicesFound xsi:type="xsd:unsignedInt">12</TotalDevicesFound>
                <CmNodes soapenc:arrayType="ns2:CmNode[1]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
                    <item xsi:type="ns2:CmNode">
                        <ReturnCode xsi:type="ns2:RisReturnCode">Ok</ReturnCode>
                        <Name xsi:type="xsd:string">10.1.240.109</Name>
                        <NoChange xsi:type="xsd:boolean">false</NoChange>
                        <CmDevices soapenc:arrayType="ns2:CmDevice[12]" xsi:type="soapenc:Array">
                            <item xsi:type="ns2:CmDevice">
                                <Name xsi:type="xsd:string">10.1.240.6</Name>
                                <DirNumber xsi:type="xsd:string" xsi:nil="true"/>
                                <Class xsi:type="ns2:DeviceClass">H323</Class>
                                <Model xsi:type="xsd:unsignedInt">62</Model>
                                <Product xsi:type="xsd:unsignedInt">17</Product>
                                <BoxProduct xsi:type="xsd:unsignedInt">0</BoxProduct>
                                <Httpd xsi:type="ns2:CmDevHttpd">No</Httpd>
                                <RegistrationAttempts xsi:type="xsd:unsignedInt">0</RegistrationAttempts>
                                <IsCtiControllable xsi:type="xsd:boolean">false</IsCtiControllable>
                                <LoginUserId xsi:type="xsd:string" xsi:nil="true"/>
                                <Status xsi:type="ns2:CmDevRegStat">Unknown</Status>
                                <StatusReason xsi:type="xsd:unsignedInt">0</StatusReason>
                                <PerfMonObject xsi:type="xsd:unsignedInt">0</PerfMonObject>
                                <DChannel xsi:type="xsd:unsignedInt">0</DChannel>
                                <Description xsi:type="xsd:string">10.1.240.6</Description>
                                <H323Trunk xsi:type="ns2:H323Trunk">
                                    <ConfigName xsi:type="xsd:string" xsi:nil="true"/>
                                    <TechPrefix xsi:type="xsd:string" xsi:nil="true"/>
                                    <Zone xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer1 xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer2 xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer3 xsi:type="xsd:string" xsi:nil="true"/>
                                    <AltGkList xsi:type="xsd:string" xsi:nil="true"/>
                                    <ActiveGk xsi:type="xsd:string" xsi:nil="true"/>
                                    <CallSignalAddr xsi:type="xsd:string" xsi:nil="true"/>
                                    <RasAddr xsi:type="xsd:string" xsi:nil="true"/>
                                </H323Trunk>
                                <TimeStamp xsi:type="xsd:unsignedInt">0</TimeStamp>
                                <Protocol xsi:type="ns2:ProtocolType">Any</Protocol>
                                <NumOfLines xsi:type="xsd:unsignedInt">0</NumOfLines>
                                <LinesStatus soapenc:arrayType="ns2:CmDevSingleLineStatus[0]" xsi:type="soapenc:Array"/>
                                <ActiveLoadID xsi:type="xsd:string" xsi:nil="true"/>
                                <InactiveLoadID xsi:type="xsd:string" xsi:nil="true"/>
                                <DownloadStatus xsi:type="ns2:DeviceDownloadStatus">Unknown</DownloadStatus>
                                <DownloadFailureReason xsi:type="xsd:string" xsi:nil="true"/>
                                <DownloadServer xsi:type="xsd:string" xsi:nil="true"/>
                                <IPAddress soapenc:arrayType="ns2:IPAddressArrayType[1]" xsi:type="soapenc:Array">
                                    <item xsi:type="ns2:IPAddressArrayType">
                                        <IP xsi:type="xsd:string">10.1.240.6</IP>
                                        <IPAddrType xsi:type="ns2:IPAddrType">ipv4</IPAddrType>
                                        <Attribute xsi:type="ns2:AttributeType">Unknown</Attribute>
                                    </item>
                                </IPAddress>
                            </item>
                            <item xsi:type="ns2:CmDevice">
                                <Name xsi:type="xsd:string">10.1.240.7</Name>
                                <DirNumber xsi:type="xsd:string" xsi:nil="true"/>
                                <Class xsi:type="ns2:DeviceClass">H323</Class>
                                <Model xsi:type="xsd:unsignedInt">62</Model>
                                <Product xsi:type="xsd:unsignedInt">17</Product>
                                <BoxProduct xsi:type="xsd:unsignedInt">0</BoxProduct>
                                <Httpd xsi:type="ns2:CmDevHttpd">No</Httpd>
                                <RegistrationAttempts xsi:type="xsd:unsignedInt">0</RegistrationAttempts>
                                <IsCtiControllable xsi:type="xsd:boolean">false</IsCtiControllable>
                                <LoginUserId xsi:type="xsd:string" xsi:nil="true"/>
                                <Status xsi:type="ns2:CmDevRegStat">Unknown</Status>
                                <StatusReason xsi:type="xsd:unsignedInt">0</StatusReason>
                                <PerfMonObject xsi:type="xsd:unsignedInt">0</PerfMonObject>
                                <DChannel xsi:type="xsd:unsignedInt">0</DChannel>
                                <Description xsi:type="xsd:string">10.1.240.7</Description>
                                <H323Trunk xsi:type="ns2:H323Trunk">
                                    <ConfigName xsi:type="xsd:string" xsi:nil="true"/>
                                    <TechPrefix xsi:type="xsd:string" xsi:nil="true"/>
                                    <Zone xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer1 xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer2 xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer3 xsi:type="xsd:string" xsi:nil="true"/>
                                    <AltGkList xsi:type="xsd:string" xsi:nil="true"/>
                                    <ActiveGk xsi:type="xsd:string" xsi:nil="true"/>
                                    <CallSignalAddr xsi:type="xsd:string" xsi:nil="true"/>
                                    <RasAddr xsi:type="xsd:string" xsi:nil="true"/>
                                </H323Trunk>
                                <TimeStamp xsi:type="xsd:unsignedInt">0</TimeStamp>
                                <Protocol xsi:type="ns2:ProtocolType">Any</Protocol>
                                <NumOfLines xsi:type="xsd:unsignedInt">0</NumOfLines>
                                <LinesStatus soapenc:arrayType="ns2:CmDevSingleLineStatus[0]" xsi:type="soapenc:Array"/>
                                <ActiveLoadID xsi:type="xsd:string" xsi:nil="true"/>
                                <InactiveLoadID xsi:type="xsd:string" xsi:nil="true"/>
                                <DownloadStatus xsi:type="ns2:DeviceDownloadStatus">Unknown</DownloadStatus>
                                <DownloadFailureReason xsi:type="xsd:string" xsi:nil="true"/>
                                <DownloadServer xsi:type="xsd:string" xsi:nil="true"/>
                                <IPAddress soapenc:arrayType="ns2:IPAddressArrayType[1]" xsi:type="soapenc:Array">
                                    <item xsi:type="ns2:IPAddressArrayType">
                                        <IP xsi:type="xsd:string">10.1.240.7</IP>
                                        <IPAddrType xsi:type="ns2:IPAddrType">ipv4</IPAddrType>
                                        <Attribute xsi:type="ns2:AttributeType">Unknown</Attribute>
                                    </item>
                                </IPAddress>
                            </item>
                            <item xsi:type="ns2:CmDevice">
                                <Name xsi:type="xsd:string">10.1.3.67</Name>
                                <DirNumber xsi:type="xsd:string" xsi:nil="true"/>
                                <Class xsi:type="ns2:DeviceClass">H323</Class>
                                <Model xsi:type="xsd:unsignedInt">62</Model>
                                <Product xsi:type="xsd:unsignedInt">17</Product>
                                <BoxProduct xsi:type="xsd:unsignedInt">0</BoxProduct>
                                <Httpd xsi:type="ns2:CmDevHttpd">No</Httpd>
                                <RegistrationAttempts xsi:type="xsd:unsignedInt">0</RegistrationAttempts>
                                <IsCtiControllable xsi:type="xsd:boolean">false</IsCtiControllable>
                                <LoginUserId xsi:type="xsd:string" xsi:nil="true"/>
                                <Status xsi:type="ns2:CmDevRegStat">Unknown</Status>
                                <StatusReason xsi:type="xsd:unsignedInt">0</StatusReason>
                                <PerfMonObject xsi:type="xsd:unsignedInt">0</PerfMonObject>
                                <DChannel xsi:type="xsd:unsignedInt">0</DChannel>
                                <Description xsi:type="xsd:string">10.1.240.118</Description>
                                <H323Trunk xsi:type="ns2:H323Trunk">
                                    <ConfigName xsi:type="xsd:string" xsi:nil="true"/>
                                    <TechPrefix xsi:type="xsd:string" xsi:nil="true"/>
                                    <Zone xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer1 xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer2 xsi:type="xsd:string" xsi:nil="true"/>
                                    <RemoteCmServer3 xsi:type="xsd:string" xsi:nil="true"/>
                                    <AltGkList xsi:type="xsd:string" xsi:nil="true"/>
                                    <ActiveGk xsi:type="xsd:string" xsi:nil="true"/>
                                    <CallSignalAddr xsi:type="xsd:string" xsi:nil="true"/>
                                    <RasAddr xsi:type="xsd:string" xsi:nil="true"/>
                                </H323Trunk>
                                <TimeStamp xsi:type="xsd:unsignedInt">0</TimeStamp>
                                <Protocol xsi:type="ns2:ProtocolType">Any</Protocol>
                                <NumOfLines xsi:type="xsd:unsignedInt">0</NumOfLines>
                                <LinesStatus soapenc:arrayType="ns2:CmDevSingleLineStatus[0]" xsi:type="soapenc:Array"/>
                                <ActiveLoadID xsi:type="xsd:string" xsi:nil="true"/>
                                <InactiveLoadID xsi:type="xsd:string" xsi:nil="true"/>
                                <DownloadStatus xsi:type="ns2:DeviceDownloadStatus">Unknown</DownloadStatus>
                                <DownloadFailureReason xsi:type="xsd:string" xsi:nil="true"/>
                                <DownloadServer xsi:type="xsd:string" xsi:nil="true"/>
                                <IPAddress soapenc:arrayType="ns2:IPAddressArrayType[1]" xsi:type="soapenc:Array">
                                    <item xsi:type="ns2:IPAddressArrayType">
                                        <IP xsi:type="xsd:string">10.1.3.67</IP>
                                        <IPAddrType xsi:type="ns2:IPAddrType">ipv4</IPAddrType>
                                        <Attribute xsi:type="ns2:AttributeType">Unknown</Attribute>
                                    </item>
                                </IPAddress>
                            </item>
</CmDevices>
                    </item>
                </CmNodes>
            </SelectCmDeviceResult>
            <ns1:StateInfo xsi:type="xsd:string">&lt;StateInfo ClusterWide=&quot;1&quot;&gt;&lt;Node Name=&quot;10.1.240.109&quot; SubsystemStartTime=&quot;1476849576&quot; StateId=&quot;100&quot; TotalItemsFound=&quot;12&quot; TotalItemsReturned=&quot;12&quot;/&gt;&lt;/StateInfo&gt;</ns1:StateInfo>
        </ns1:selectCmDeviceResponse>
    </soapenv:Body>
</soapenv:Envelope> 

谢谢

尝试这样的事情

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> selectCmDeviceResult = doc.Descendants().Where(x => x.Name.LocalName == "SelectCmDeviceResult").ToList();

            var results = selectCmDeviceResult.Select(x => new {
                selectCmDeviceResult = (int)x.Element("TotalDevicesFound"),
                item = x.Element("CmNodes").Elements("item").Select(y => new {
                    returnCode = (string)y.Element("ReturnCode"),
                    name = (string)y.Element("Name"),
                    noChange = (Boolean)y.Element("NoChange"),
                    items = y.Element("CmDevices").Elements("item").Select(z => new {
                        name = (string)z.Element("Name"),
                        dirNumber = (string)z.Element("DirNumber"),
                        _class = (string)z.Element("Class"),
                        model = (int)z.Element("Model"),
                        product = (int)z.Element("Product"),
                        boxProduct = (int)z.Element("BoxProduct"),
                        httpd = (string)z.Element("Httpd"),
                        registrationAttempts = (int)z.Element("RegistrationAttempts"),
                        isCtiControllable = (Boolean)z.Element("IsCtiControllable"),
                        loginUserId = (string)z.Element("LoginUserId"),
                        status = (string)z.Element("Status"),
                        statusReason = (string)z.Element("StatusReason"),
                        perfMonObject = (int)z.Element("PerfMonObject"),
                        dChannel = (int)z.Element("DChannel"),
                        description = (string)z.Element("Description"),
                        timeStamp = (int)z.Element("TimeStamp"),
                        protocol = (string)z.Element("Protocol"),
                        numOfLines = (int)z.Element("NumOfLines"),
                        ipAddress = z.Elements("IPAddress").Select(a => new {
                            ip = (string)a.Descendants("IP").FirstOrDefault(),
                            ipAddressType = (string)a.Descendants("IPAddrType").FirstOrDefault(),
                            attribute = (string)a.Descendants("Attribute").FirstOrDefault()
                        }).FirstOrDefault()
                    }).ToList()
                }).FirstOrDefault()
            }).FirstOrDefault();

        }
    }
}