Solved

Finding the Child Node Number in an XML file

Posted on 2008-10-02
4
1,300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 60

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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