How to extract child nodes from specific xml file

Posted on 2011-10-27
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
    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 29

    Accepted Solution

    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

    Thanks! Just what I needed.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    return a file info path 20 33
    WiX Installer 2 37
    return array from dll 13 19
    string separate in two variables c# 5 34
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now