Solved

Finding the Child Node Number in an XML file

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Transaction Number 19 49
Using sample Autorize.net c# simple example 1 53
Run software updates from the website 6 50
Need Help With GDI+ in VB.Net 8 40
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…
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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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