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

664 views Asked by At

we did a soap request to cisco cucm and got this result. we need to read this result and get the details of each device (item node). problem is there is multiple nodes with same name "item" what is the best way to read it?

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

thanks

1

There are 1 answers

1
jdweng On BEST ANSWER

Try something like this

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();

        }
    }
}