Childnodes property reports only one node...

Discussion in 'ASP .Net' started by Charlie@CBFC, Dec 13, 2006.

  1. Charlie@CBFC

    Charlie@CBFC Guest

    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
    Charlie@CBFC, Dec 13, 2006
    #1
    1. Advertising

  2. On Wed, 13 Dec 2006 13:02:32 -0500, "Charlie@CBFC"
    <> wrote:

    >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.

    >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
    >
    Registered User, Dec 13, 2006
    #2
    1. Advertising

  3. 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



    "Charlie@CBFC" <> wrote in message
    news:%...
    > 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
    >
    Mark Fitzpatrick, Dec 13, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,401
  2. =?Utf-8?B?Unlv?=

    Select childnodes of a checked node on treeview ?

    =?Utf-8?B?Unlv?=, Dec 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,035
    Robbe Morris [C# MVP]
    Dec 30, 2005
  3. lb
    Replies:
    7
    Views:
    10,513
    TechBookReport
    Apr 28, 2004
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,401
    Dimitre Novatchev
    Aug 24, 2006
  5. lmg

    xml node.childNodes.length

    lmg, Apr 1, 2008, in forum: Javascript
    Replies:
    1
    Views:
    198
Loading...

Share This Page