Solved

Finding the Child Node Number in an XML file

Posted on 2008-10-02
4
1,294 Views
Last Modified: 2012-05-05
In my ASP.Net 2.0 code I am referencing different nodes within an XML file in order to extract the data and write it to a database. This results in code like below;

Dim BF As String = Node.ChildNodes(4).ChildNodes(0).ChildNodes(1).InnerText

My question: I find it near impossible at times to count my way down a massive node-tree in an XML file... is there any way of getting this information automatically - or software which does it?

Thanks
0
Comment
Question by:footballencounters
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22623136
It is usually done how it appears you are doing it by getting the child nodes and testing what number child it is then using that in your code like above (AFAIK).

Couple of things to try, is the child node selected unique -- capable of being identified aside from its order in the XML document?  If so, you can try using XPATH to select the specific node.

Otherwise, you can use the FirstChild/LastChild properties if for example node 4 is the last child of Node.  If you always want index 0 of Node.ChildNodes(4) then FirstChild works well.

Here is a listing of the XmlNode class members: http://msdn.microsoft.com/en-us/library/system.xml.xmlnode_members(VS.71).aspx

Hopefully that helps

Regards,
Kevin
0
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
ID: 22623156
HI,

I am not sure what exaclty you want to read from your file but here is a generic funtion for you that will read you xml data. You can also visit http://www.developer.com/net/csharp/article.php/3489611 to learn on how to manipulate xml data.

     
    ReadNewsLetterXml(Server.MapPath("NewsLetter.xml"));

 

    public void ReadNewsLetterXml(string path)
    {
        System.Xml.XmlDocument Document = new System.Xml.XmlDocument();
        Document.Load(path);

        if (Document.HasChildNodes)
        {
            // This line below will enable you to read your full xml file
            ReadXmlNodes(Document.ChildNodes);
           
            // Uncomment the line below if you want to read article nodes
            // ReadXmlNodes(Document.SelectSingleNode("newsletter/items[@value='article']").ChildNodes);

            // Uncomment the line below if you want to read comic nodes
            // ReadXmlNodes(Document.SelectSingleNode("newsletter/items[@value='comics']").ChildNodes);

        }
    }


    /// <summary>
    /// This function will read the xmlnodelist selected
    /// </summary>
    /// <param name="nodeList"></param>
    public void ReadXmlNodes(System.Xml.XmlNodeList nodeList)
    {
        foreach (System.Xml.XmlNode n in nodeList)
        {
            if (n.ChildNodes.Count == 0)
            {
                Response.Write(n.InnerText);
                Response.Write("<br>");
            }
            else
            {
                if (n.Attributes.Count > 0)
                {
                    Response.Write("<b>" + n.Attributes[0].Value + "</b><br>");
                }
                ReadXmlNodes(n.ChildNodes);
            }
        }
    }
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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