Solved

Finding the Child Node Number in an XML file

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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