Solved

Finding the Child Node Number in an XML file

Posted on 2008-10-02
4
1,290 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

15 Experts available now in Live!

Get 1:1 Help Now