Sanjana
Sanjana

Reputation: 467

Unable to fetch a node value from string XML

I am trying to load particular values from a XML which is stored in a string XML. But for some reason, the node is not getting picked up.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<megaServiceResponse xmlns="http://www.asbcsda/ms">
    <serviceDetails>
        <site>
            <siteAccountNumber>123</siteAccountNumber>
            <serviceAddress>
                <streetAddress>abc</streetAddress>
                <city>Rockaway</city>
                <state>NJ</state>
                <zip>07866</zip>
            </serviceAddress>
            <line>
                <lsn>279542</lsn>
                <billcode>NCB02HW51C</billcode>
                <siteAccountId>2230066</siteAccountId>
                <backupParentLsn>0</backupParentLsn>
                <dateConnected>2012-06-07 07:29:33.0</dateConnected>
                <lineType>Dedicated Lines</lineType>
                <orderType>ESA</orderType>
                <primaryOrBackupLine>No Backup/Failover LSN</primaryOrBackupLine>
                <mplsOrdered>false</mplsOrdered>
                <ipSecOrdered>false</ipSecOrdered>
                <failoverOrdered>false</failoverOrdered>
                <dbuServiceOrdered>false</dbuServiceOrdered>
                <lsnClli>DNVLNJRK</lsnClli>
                <serviceProducts>
                    <serviceProduct>
                        <serviceType>Enhanced IP</serviceType>
                        <productName>/29 IP Address Block (5 useable)</productName>
                        <quantity>1</quantity>
                    </serviceProduct>
                    <serviceProduct>
                        <serviceType>Enterprise</serviceType>
                        <productName>T1 1.5 (ESA8)</productName>
                        <quantity>1</quantity>
                    </serviceProduct>
                </serviceProducts>
            </line>
        </site>
    </serviceDetails>
</megaServiceResponse>

The nodes which I want to fetch is /megaServiceResponse/serviceDetails/site/line/serviceProducts/serviceProduct

I am using the following code

         XmlDocument doc = new XmlDocument();
         doc.LoadXml(json); //json is an XML string
         var nodes = doc.SelectNodes("/megaServiceResponse/serviceDetails/site/line/serviceProducts/serviceProduct");

The above line is returning null node.

Upvotes: 0

Views: 86

Answers (1)

dario
dario

Reputation: 5269

This is because you have a namespace in the XML. So you either remove it and use your code, like this:

<megaServiceResponse>
    <serviceDetails>

Or you need to declare it before selecting the nodes, like this:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("mp", "http://www.abcdesg.com/ms");

var nodes = doc.SelectNodes("//mp:megaServiceResponse/mp:serviceDetails/mp:site/mp:line/mp:serviceProducts/mp:serviceProduct", nsmgr);

Upvotes: 2

Related Questions