Results 1 to 3 of 3

Thread: [RESOLVED] Traversing XML (Recursion)

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [RESOLVED] Traversing XML (Recursion)

    I wrote a small bit of code to traverse through all the nodes of a XML document. I am facing an issue with output.
    This is my source:
    XML Code:
    1. <?xml version='1.0' encoding='UTF-8'?>
    2. <QUOTE_HEADER>
    3.     <QUOTE_NO>100</QUOTE_NO>
    4.     <QUOTE_LINES>
    5.         <QUOTE_ADTL><ITEM>1</ITEM><DESC>Magician box</DESC></QUOTE_ADTL>
    6.         <QUOTE_FURNITURE><ITEM>001</ITEM><DESC>Electric Chair</DESC></QUOTE_FURNITURE>
    7.     </QUOTE_LINES>
    8.     <DELIVERY_ADDRESS>
    9.     <ADDRESS>100 AVENUE OF THE AMERICAS</ADDRESS>
    10.         <CITY>NEW YORK</CITY>
    11.         <STATE>NEW YORK</STATE>
    12.         <ZIP>10013-1689</ZIP>
    13.     </DELIVERY_ADDRESS>
    14. </QUOTE_HEADER>

    This is my code:
    c# Code:
    1. using System;
    2. using System.Xml;
    3.  
    4. namespace StripMe
    5. {
    6.     class Class1
    7.     {
    8.         public static int GetNodeTypeCount(XmlNode node, XmlNodeType nodeType)
    9.         {
    10.             // Recursively loop through the given node and return
    11.             // the number of occurences of a specific nodeType.
    12.             int i = 0;
    13.  
    14.             if (node.NodeType == nodeType)
    15.                 i = i + 1;
    16.  
    17.             if (node.HasChildNodes)
    18.                 foreach (XmlNode cNode in node.ChildNodes)
    19.                 {
    20.                     i = i + GetNodeTypeCount(cNode, nodeType);
    21.                     Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
    22.                 }  
    23.             return i;
    24.         }
    25.  
    26.         [STAThread]
    27.         static void Main(string[] args)
    28.         {
    29.             try
    30.             {
    31.                 XmlDocument doc = new XmlDocument();
    32.                 doc.Load(args[0]);
    33.                 XmlNodeList nodeList = doc.GetElementsByTagName("*");
    34.                 Class1.GetNodeTypeCount(doc.DocumentElement, XmlNodeType.Element);
    35.                 Console.ReadLine();
    36.             }
    37.             catch (XmlException xmlEx)        // Handle the XML Exceptions here.        
    38.             {
    39.                 Console.WriteLine("{0}", xmlEx.Message);
    40.             }
    41.             catch (Exception ex)              // Handle the Generic Exceptions here.
    42.             {
    43.                 Console.WriteLine("{0}", ex.Message);
    44.             }
    45.         }
    46.     }
    47. }
    This is my output:
    Code:
    QUOTE_NO//#text
    QUOTE_HEADER//QUOTE_NO
    ITEM//#text
    QUOTE_ADTL//ITEM
    DESC//#text
    QUOTE_ADTL//DESC
    QUOTE_LINES//QUOTE_ADTL
    ITEM//#text
    QUOTE_FURNITURE//ITEM
    DESC//#text
    QUOTE_FURNITURE//DESC
    QUOTE_LINES//QUOTE_FURNITURE
    QUOTE_HEADER//QUOTE_LINES
    ADDRESS//#text
    DELIVERY_ADDRESS//ADDRESS
    CITY//#text
    DELIVERY_ADDRESS//CITY
    STATE//#text
    DELIVERY_ADDRESS//STATE
    ZIP//#text
    DELIVERY_ADDRESS//ZIP
    QUOTE_HEADER//DELIVERY_ADDRESS
    I would like to get the output without the #text coming up. What do I need to do? Any ideas?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Traversing XML (Recursion)

    Try replacing with this where you are printing ParentName and Name:
    csharp Code:
    1. if(cNode.NodeType == XmlNodeType.Text)
    2.     Console.WriteLine(cNode.ParentNode.Name);
    3. else
    4.     Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Traversing XML (Recursion)

    Quote Originally Posted by Harsh Gupta View Post
    Try replacing with this where you are printing ParentName and Name:
    csharp Code:
    1. if(cNode.NodeType == XmlNodeType.Text)
    2.     Console.WriteLine(cNode.ParentNode.Name);
    3. else
    4.     Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
    Harsh,
    Thanks for your solution. I changed it to this:
    csharp Code:
    1. public static int GetNodeTypeCount(XmlNode node, XmlNodeType nodeType)
    2.         {
    3.             // Recursively loop through the given node and return
    4.             // the number of occurences of a specific nodeType.
    5.             int i = 0;
    6.  
    7.             if (node.NodeType == nodeType)
    8.                 i = i + 1;
    9.  
    10.             if (node.HasChildNodes)
    11.                 foreach (XmlNode cNode in node.ChildNodes)
    12.                 {
    13.                     i = i + GetNodeTypeCount(cNode, nodeType);
    14.                     if (cNode.NodeType == XmlNodeType.Element)
    15.                         Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
    16.                 }  
    17.             return i;
    18.         }
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width