Childnodes property reports only one node...

C

Charlie@CBFC

Hi:

I'm trying to loop through an XML document but ChildNodes property reports
only one node, but there are many. XML document is valid. What am I doing
wrong?

foreach( XmlNode node in xml.ChildNodes ) { }

Below is XML...

<Catalog>
<Category>
<CategoryName>Drivers</CategoryName>
<Subcategory>
<SubcategoryName>Tweeters</SubcategoryName>
<SubCategorySubCategory>
<SubCategorySubCategoryName>.5 inch Dome
Tweeters</SubCategorySubCategoryName>
<Product>
<ShortDescription>.5 Metal Dome Tweeter</ShortDescription>
<Price>25.9600</Price>
</Product>
</SubCategorySubCategory>
<SubCategorySubCategory>
<SubCategorySubCategoryName>.75 inch Dome
Tweeters</SubCategorySubCategoryName>
<Product>
<ShortDescription>.75 Fabric Dome Tweeter</ShortDescription>
<Price>28.9900</Price>
</Product>
</SubCategorySubCategory>
<SubCategorySubCategory>
<SubCategorySubCategoryName>1 inch Dome
Tweeters</SubCategorySubCategoryName>
<Product>
<ShortDescription>1 Fabric Dome Tweeter</ShortDescription>
<Price>55.0000</Price>
</Product>
<Product>
<ShortDescription>1 Meal Dome Tweeter</ShortDescription>
<Price>44.0000</Price>
</Product>
</SubCategorySubCategory>
</Subcategory>
<Subcategory>
<SubcategoryName>Mid-Ranges</SubcategoryName>
<SubCategorySubCategory>
<SubCategorySubCategoryName>5.25 inch
Mid-Ranges</SubCategorySubCategoryName>
<Product>
<ShortDescription>5.25 Poly Mid-range</ShortDescription>
<Price>55.0000</Price>
</Product>
</SubCategorySubCategory>
<SubCategorySubCategory>
<SubCategorySubCategoryName>3 inch Dome
Mid-Ranges</SubCategorySubCategoryName>
<Product>
<ShortDescription>3 Metal Dome Mid-range</ShortDescription>
<Price>89.5000</Price>
</Product>
</SubCategorySubCategory>
</Subcategory>
<Subcategory>
<SubcategoryName>Woofers</SubcategoryName>
<SubCategorySubCategory>
<SubCategorySubCategoryName>6.5 inch Woofers</SubCategorySubCategoryName>
<Product>
<ShortDescription>6.5 Paper Cone Woofer</ShortDescription>
<Price>125.0000</Price>
</Product>
</SubCategorySubCategory>
<SubCategorySubCategory>
<SubCategorySubCategoryName>10 inch Woofers</SubCategorySubCategoryName>
<Product>
<ShortDescription>10 Poly Cone Woofer</ShortDescription>
<Price>175.0000</Price>
</Product>
</SubCategorySubCategory>
</Subcategory>
</Category>
<Category>
<CategoryName>Electrical Components</CategoryName>
<Subcategory>
<SubcategoryName>Resistors</SubcategoryName>
<SubCategorySubCategory>
<SubCategorySubCategoryName>Metal Film
Resistors</SubCategorySubCategoryName>
<Product>
<ShortDescription>220 ohm Metal Film</ShortDescription>
<Price>0.2500</Price>
</Product>
<Product>
<ShortDescription>300 ohm Metal Film</ShortDescription>
<Price>0.2500</Price>
</Product>
<Product>
<ShortDescription>500 ohm Metal Film</ShortDescription>
<Price>0.2500</Price>
</Product>
</SubCategorySubCategory>
<SubCategorySubCategory>
<SubCategorySubCategoryName>Carbon Resistors</SubCategorySubCategoryName>
<Product>
<ShortDescription>300 ohm Carbon</ShortDescription>
<Price>0.1500</Price>
</Product>
<Product>
<ShortDescription>220 ohm Carbon</ShortDescription>
<Price>0.1500</Price>
</Product>
<Product>
<ShortDescription>500 ohm Carbon</ShortDescription>
<Price>0.1500</Price>
</Product>
</SubCategorySubCategory>
</Subcategory>
<Subcategory>
<SubcategoryName>Capacitors</SubcategoryName>
<SubCategorySubCategory>
<SubCategorySubCategoryName>Bi-Polar Capacitors</SubCategorySubCategoryName>
<Product>
<ShortDescription>10 uF Bi-Polar</ShortDescription>
<Price>0.1500</Price>
</Product>
<Product>
<ShortDescription>12 uF Bi-Polar</ShortDescription>
<Price>0.1500</Price>
</Product>
<Product>
<ShortDescription>15 uF Bi-Polar</ShortDescription>
<Price>0.1500</Price>
</Product>
</SubCategorySubCategory>
<SubCategorySubCategory>
<SubCategorySubCategoryName>Non-Polarized
Capacitors</SubCategorySubCategoryName>
<Product>
<ShortDescription>.15 uF Poly</ShortDescription>
<Price>0.9900</Price>
</Product>
<Product>
<ShortDescription>.35 uF Poly</ShortDescription>
<Price>1.5000</Price>
</Product>
<Product>
<ShortDescription>.0015 uF Poly</ShortDescription>
<Price>2.9500</Price>
</Product>
</SubCategorySubCategory>
</Subcategory>
</Category>
</Catalog>

Thanks,
Charlie
 
R

Registered User

Hi:

I'm trying to loop through an XML document but ChildNodes property reports
only one node, but there are many. XML document is valid. What am I doing
wrong?

foreach( XmlNode node in xml.ChildNodes ) { }
I suspect the xml object is of type XmlDocument and the one child node
found is named Catalog. Examine the XmlDocument.DocumentNode property.

regards
A.G.
 
M

Mark Fitzpatrick

Keep in mind, you're not dealing with a collection of nodes, you're dealing
with a hierarchical collection of nodes. The document has one childnode,
Catalog, when viewed from the root. If you then set then set the parent node
to be that particular childnode, you'll find a number of childnodes equal to
the number of category elements in the document and so on. To really go
through the document object you'll have to use a recursive function that
basically receives a childnode property which then becomes the parent node
for that function. The funciton then iterates through all the childnodes and
performs some operation (like dumping the value to a trace) and then calls
itself, passing that particular childnode to the function to become a parent
node to find all it's children.

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
 

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top