XML Parser returning all childnodes rather than single level nodes ofchild in hand

I

Iain

Hi All

Please excuse my ignorance people but this is my first foray into XML
and attempting to parse an xml document.

The following is a small sample of a very large XML file that is being
used to pass data regarding a product's configuration.
All I am trying to day is read the data and write it to a listbox as a
start. I ultimately will write the data to a data table.


I suspect that it is the way I am retrieving the child nodes (see the
marker in the files).

Any assistance will be greatly received

Iain


==============================================


Part of the xml file I am trying to parse.

<MID>
<VERSION>3.0</VERSION>
<ORDER code="AJOH99999999">
<STATUS>VALID</STATUS>
<COUNTRY>UK</COUNTRY>
<DISPLAY>
<POSITION>
<POSITION P="1">L1</POSITION>
<POSITION P="2">R1</POSITION>
</POSITION>
<TOTECOUNT>
<POSITION P="1">0</POSITION>
<POSITION P="2">0</POSITION>
</TOTECOUNT>
<TYPE>
<POSITION P="1">LCD</POSITION>
<POSITION P="2">LCD</POSITION>
</TYPE>
</DISPLAY>

**************************************************************************************
The following is the output of the progress

1<MID>
2<VERSION>3.0
2</VERSION>
2<ORDER code="AJOH99999999">
3<STATUS>VALID
3</STATUS>
3<COUNTRY>UK
3</COUNTRY>
3<DISPLAY>
4<POSITION>
5<POSITION P="1">L1
5</POSITION>
5<POSITION P="2">R1
5</POSITION>
4</POSITION>

// Start additional Lines */
4<POSITION P="1">L1
4</POSITION>
4<POSITION P="2">R1
4</POSITION>
4</POSITION>
// End Of additional Lines */

4</POSITION>
4<TOTECOUNT>
5<POSITION P="1">0
5</POSITION>
5<POSITION P="2">0
5</POSITION>
4</TOTECOUNT>

// Start additional Lines */
4<POSITION P="1">0
4</POSITION>
4<POSITION P="2">0
4</POSITION>
// End Of additional Lines */

4<TYPE>
5<POSITION P="1">LCD
5</POSITION>
5<POSITION P="2">LCD
5</POSITION>
4</TYPE>

// Start additional Lines */
4<POSITION P="1">LCD
4</POSITION>
4<POSITION P="2">LCD
4</POSITION>
// End Of additional Lines */


3</DISPLAY>


// Additional Lines
3<POSITION>
4<POSITION P="1">L1
4</POSITION>
4<POSITION P="2">R1
4</POSITION>
3</POSITION>
3<POSITION P="1">L1
3</POSITION>
3<POSITION P="2">R1
3</POSITION>

3<TOTECOUNT>
4<POSITION P="1">0
4</POSITION>
4<POSITION P="2">0
4</POSITION>
3</TOTECOUNT>
3<POSITION P="1">0
3</POSITION>
3<POSITION P="2">0
3</POSITION>

3<TYPE>
4<POSITION P="1">LCD
4</POSITION>
4<POSITION P="2">LCD
4</POSITION>
3</TYPE>
3<POSITION P="1">LCD
3</POSITION>
3<POSITION P="2">LCD
3</POSITION>


**************************************************************************************

The following is my first attempt at recursive code that I am using


public void parseTheTree(XmlNode theNode, XmlNamespaceManager
nsmgr, int theLevelNumber)
{
XmlNodeList theChildNodes = null;
string theCodeAtttribute = "";
string thePAttribute = "";
string theValue = "";
string theName = "";
string ldebugStart = "";
string ldebugEnd = "";

if (! theNode.HasChildNodes)
{
return;
}
else
{
theLevelNumber = theLevelNumber + 1;
theCodeAtttribute = getTheCodeAttributes(theNode, nsmgr);
thePAttribute = getThePAttributes(theNode, nsmgr);
theName = getTheNodeName(theNode, nsmgr);
theValue = getTheNodeValue(theNode, nsmgr);

try
{
if ((theCodeAtttribute == "") && (thePAttribute == ""))
{
if (theNode.HasChildNodes)
{
listBox1.Items.Add(theLevelNumber.ToString() + "<" +
theName + ">");
}
else
{
listBox1.Items.Add(theLevelNumber.ToString() + "<" +
theName + ">" + theValue);
}
}
else
{
if (theCodeAtttribute != "")
{
listBox1.Items.Add(theLevelNumber.ToString() + "<" +
theName + " code=\"" + theCodeAtttribute + "\"" + ">");
}
if (thePAttribute != "")
{
listBox1.Items.Add(theLevelNumber.ToString() + "<" +
theName + " P=\"" + thePAttribute + "\"" + ">" + theValue);
}
}
}
catch
{
listBox1.Items.Add("Error Parsing XML Document");
}


/* I suspect that this is wrong and that I am retrieving all
child nodes rather than the child nodes of the node currently in hand
*/
theChildNodes = theNode.SelectNodes("descendant::*");
foreach (XmlNode theChild in theChildNodes)
{
parseTheTree(theChild, nsmgr, theLevelNumber);
}
listBox1.Items.Add(theLevelNumber.ToString() + "</" +
theNode.Name.ToString() + ">");
}
}
 
M

Martin Honnen

Iain said:
/* I suspect that this is wrong and that I am retrieving all
child nodes rather than the child nodes of the node currently in hand
*/
theChildNodes = theNode.SelectNodes("descendant::*");

First of all in the DOM model there already is a property named
ChildNodes if you want to access the child node so you probably simply want
foreach (XmlNode theChild in theNode.ChildNodes)
Or if you want to use XPath and SelectNodes then use
foreach (XmlNode theChild in theNode.SelectNodes("node()"))
to process all kind of child nodes or alternatively
foreach (XmlNode theChild in theNode.SelectNodes("*"))
to process all element child nodes.
 
A

Andy O'Neill

Iain said:
Hi All

Please excuse my ignorance people but this is my first foray into XML
and attempting to parse an xml document.

The following is a small sample of a very large XML file that is being
used to pass data regarding a product's configuration.
All I am trying to day is read the data and write it to a listbox as a
start. I ultimately will write the data to a data table.


I suspect that it is the way I am retrieving the child nodes (see the
marker in the files).

Any assistance will be greatly received

I would use linq to xml.
You could write a method that took an xelement and looped through all the
xelements a level below and did stuff with selecting and attributes or
whatever it is you want to do..

What exactly is wrong with what you have though?
And what should your output be?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top