How to extract child nodes from specific xml file

Posted on 2011-10-27
Medium Priority
Last Modified: 2012-08-13
Hi All,

I was just given the sample xml file, and I'm having trouble pulling the child nodes with in it. I can loop the node, but it will only goto the first child node for each location. I need to extract the  NAME value from "<SUBLOCATION ID="SFDF22D7" NAME="202743"/>"

sample code below.

           foreach (XmlNode appNode in docNav.SelectNodes("/COMPANYINFO/CORPORATE /LOCATION "))

                // Returns XmlNode.InnerText but its blank.
                sInactiveProjects[counter] = GetNodeContents(appNode, "LOCATION").ToString();

Sample xml file below.

        <LOCATION ID="P88972FC" NAME="EASIER">
            <SUBLOCATION ID="S89872FE" NAME="400462"/>
        <LOCATION ID="PDRD2905" NAME="XT-454560-5">
            <SUBLOCATION ID="SFDF22D7" NAME="202743"/>
            <SUBLOCATION ID="S106B993E" NAME="401048"/>
            <SUBLOCATION ID="SDEA2907" NAME="507152"/>
            <SUBLOCATION ID="123C699" NAME="507180"/>
            <SUBLOCATION ID="HTY4C896" NAME="507205"/>
            <SUBLOCATION ID="SFD580DD" NAME="600281"/>
        <LOCATION ID="PD1104ED" NAME="TD-454560-4">
            <SUBLOCATION ID="211704EF" NAME="202724"/>
            <SUBLOCATION ID="SFD37943" NAME="401056"/>
            <SUBLOCATION ID="XDEA2A2D" NAME="507169"/>
Question by:mlong219
LVL 18

Expert Comment

by:Anil Golamari
ID: 37040918
try using something like


here first node will be that of Corporate ID
2nd: locationID
3rd sublocationID

Here after entering the sublocation ID try to get the required element either using the ID name or chilenode number.
LVL 30

Accepted Solution

anarki_jimbel earned 2000 total points
ID: 37042613
Try this. Code pretty self-explanatory.

Location = EASIER; sublocation = 400462
Location = XT-454560-5; sublocation = 202743
Location = XT-454560-5; sublocation = 401048
Location = XT-454560-5; sublocation = 507152
Location = XT-454560-5; sublocation = 507180
Location = XT-454560-5; sublocation = 507205
Location = XT-454560-5; sublocation = 600281
Location = TD-454560-4; sublocation = 202724
Location = TD-454560-4; sublocation = 401056
Location = TD-454560-4; sublocation = 507169
private void button2_Click(object sender, EventArgs e)
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

            XmlNodeList locations = doc.GetElementsByTagName("LOCATION");
            foreach (XmlElement location in locations)
                XmlAttribute locationNameAttribute = location.Attributes["NAME"];
                string locationName = locationNameAttribute.Value;
                XmlNodeList sublocations = location.GetElementsByTagName("SUBLOCATION");
                // Returns XmlNode.InnerText but its blank.
                foreach (XmlElement sublocation in sublocations)
                    XmlAttribute nameAttribute = sublocation.Attributes["NAME"];
                    string name = nameAttribute.Value;
                    System.Diagnostics.Debug.WriteLine("Location = " + locationName + "; sublocation = " + name);

Open in new window


Author Closing Comment

ID: 37043112
Thanks! Just what I needed.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question