|
-
Feb 17th, 2010, 05:24 PM
#1
[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:
<?xml version='1.0' encoding='UTF-8'?>
<QUOTE_HEADER>
<QUOTE_NO>100</QUOTE_NO>
<QUOTE_LINES>
<QUOTE_ADTL><ITEM>1</ITEM><DESC>Magician box</DESC></QUOTE_ADTL>
<QUOTE_FURNITURE><ITEM>001</ITEM><DESC>Electric Chair</DESC></QUOTE_FURNITURE>
</QUOTE_LINES>
<DELIVERY_ADDRESS>
<ADDRESS>100 AVENUE OF THE AMERICAS</ADDRESS>
<CITY>NEW YORK</CITY>
<STATE>NEW YORK</STATE>
<ZIP>10013-1689</ZIP>
</DELIVERY_ADDRESS>
</QUOTE_HEADER>
This is my code:
c# Code:
using System;
using System.Xml;
namespace StripMe
{
class Class1
{
public static int GetNodeTypeCount(XmlNode node, XmlNodeType nodeType)
{
// Recursively loop through the given node and return
// the number of occurences of a specific nodeType.
int i = 0;
if (node.NodeType == nodeType)
i = i + 1;
if (node.HasChildNodes)
foreach (XmlNode cNode in node.ChildNodes)
{
i = i + GetNodeTypeCount(cNode, nodeType);
Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
}
return i;
}
[STAThread]
static void Main(string[] args)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(args[0]);
XmlNodeList nodeList = doc.GetElementsByTagName("*");
Class1.GetNodeTypeCount(doc.DocumentElement, XmlNodeType.Element);
Console.ReadLine();
}
catch (XmlException xmlEx) // Handle the XML Exceptions here.
{
Console.WriteLine("{0}", xmlEx.Message);
}
catch (Exception ex) // Handle the Generic Exceptions here.
{
Console.WriteLine("{0}", ex.Message);
}
}
}
}
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
-
Feb 17th, 2010, 11:39 PM
#2
Re: Traversing XML (Recursion)
Try replacing with this where you are printing ParentName and Name:
csharp Code:
if(cNode.NodeType == XmlNodeType.Text) Console.WriteLine(cNode.ParentNode.Name); else Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
-
Feb 18th, 2010, 09:02 AM
#3
Re: Traversing XML (Recursion)
 Originally Posted by Harsh Gupta
Try replacing with this where you are printing ParentName and Name:
csharp Code:
if(cNode.NodeType == XmlNodeType.Text)
Console.WriteLine(cNode.ParentNode.Name);
else
Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
Harsh,
Thanks for your solution. I changed it to this:
csharp Code:
public static int GetNodeTypeCount(XmlNode node, XmlNodeType nodeType)
{
// Recursively loop through the given node and return
// the number of occurences of a specific nodeType.
int i = 0;
if (node.NodeType == nodeType)
i = i + 1;
if (node.HasChildNodes)
foreach (XmlNode cNode in node.ChildNodes)
{
i = i + GetNodeTypeCount(cNode, nodeType);
if (cNode.NodeType == XmlNodeType.Element)
Console.WriteLine(cNode.ParentNode.Name + "//" +cNode.Name);
}
return i;
}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|