Loop through sub XmlNodeList with C#http://forums.asp.net/t/1097714.aspx/1?Loop+through+sub+XmlNodeList+with+C+Tue, 22 Nov 2011 08:30:38 -050010977141661122http://forums.asp.net/p/1097714/1661122.aspx/1?Loop+through+sub+XmlNodeList+with+C+Loop through sub XmlNodeList with C# HI all, <p>I've searched for an answer to this, as I'm sure it will have been asked before, but can't find anything. It's quite a simple query..</p> <p>I'm reading from a XML feed from a URL and can successfully loop through a node list for a top level node. However, within the Article node is a node called Categories, as an article can be associated with multiple categories. I then need to loop through this node list within each Article. In VB, I can do this as below, but I need to convert this to C# and can't find the equivalent</p> <p>VB.NET:</p> <pre class="prettyprint">Dim xtr As XmlTextReader = New XmlTextReader(&quot;http://feed.url.com&quot;) xtr.WhitespaceHandling = WhitespaceHandling.None Dim X As New XmlDocument X.Load(xtr) If Not X Is Nothing Then Try Dim ArticleList As XmlNodeList = X.SelectNodes(&quot;InfoStreamResults/Article&quot;) Dim Article As XmlNode For Each Article In ArticleList Dim CatNodesList As XmlNodeList = Article.SelectNodes(&quot;Categories/Category&quot;) Dim category As XmlNode For Each category In CatNodesList ' do stuff, i.e. If Not Article.SelectSingleNode(&quot;nodename&quot;) Is Nothing Then nodename= safeEncode(Article.SelectSingleNode(&quot;nodename&quot;).Value) End If Next Next Catch ex As Exception ' exception End Try End If</pre> <p>&nbsp;</p> <p>II can do the outer loop in C# fine, but when it comes to the equivalent of lines 17 to 28 as above, this DOESN'T work;</p> <p>C#:</p> <p><strong>1&nbsp;&nbsp;&nbsp; </strong><span class="kwd">foreach</span> (XmlNode category <span class="kwd"> in</span> Article.SelectNodes(<span class="st">&quot;Categories/Category&quot;</span>))<br> <b id="2">2&nbsp;&nbsp;&nbsp; </b>{<br> <b id="3">3&nbsp;&nbsp;&nbsp; </b>&nbsp;&nbsp; categoryID = category.SelectSingleNode(<span class="st">&quot;@ID&quot;</span>).Value;<br> <b id="4">4&nbsp;&nbsp;&nbsp; </b>&nbsp;&nbsp; categoryName = EncodeIt(category.SelectSingleNode(<span class="st">&quot;category&quot;</span>).InnerText);<br> <b id="5">5&nbsp;&nbsp;&nbsp; </b>}<br> </p> <p>So how do I loop through a set of sub nodes within a node using foreach as I do in VB.NET, but in C#?</p> <p>Thanks, hope that makes sense.</p> 2007-04-11T14:49:35-04:001661785http://forums.asp.net/p/1097714/1661785.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Try the below code</p> <font size="2"></font><font color="#0000ff" size="2"><font size="2"> <p></font><font color="#0000ff" size="2">if</font><font size="2">(X != </font><font color="#0000ff" size="2">null</font><font size="2">)</p> <p>{</p> <p></font><font color="#0000ff" size="2">try</p> </font><font size="2"> <p>{</p> <p>XmlNodeList ArticleList = X.SelectNodes(&quot;InfoStreamResults/Article&quot;);</p> <p></font><font color="#0000ff" size="2">foreach</font><font size="2">(XmlNode Article </font><font color="#0000ff" size="2">in</font><font size="2"> ArticleList)</p> <p>{</p> <p>XmlNodeList CatNodesList = Article.SelectNodes(&quot;Categories/Category&quot;);</p> <p></font><font color="#0000ff" size="2">foreach</font><font size="2">( XmlNode category </font><font color="#0000ff" size="2">in</font><font size="2"> CatNodesList)</p> <p>{</p> <p></font><font color="#008000" size="2">//do stuff</p> </font><font size="2"> <p></font><font color="#0000ff" size="2">if</font><font size="2">(Article.SelectSingleNode(&quot;nodename&quot;) != </font><font color="#0000ff" size="2">null</font><font size="2">)</p> <p>{</p> <p>nodename = safeEncode(Article.SelectSingleNode(&quot;nodename&quot;).Value);</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p></font><font color="#0000ff" size="2">catch</p> </font><font size="2"> <p>{</p> <p>}</p> <p>}</p> </font></font><font size="2"> <p>HC</p> </font> 2007-04-11T21:51:38-04:001663515http://forums.asp.net/p/1097714/1663515.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:&nbsp;</p> <pre class="prettyprint">XmlTextReader xtr = new XmlTextReader(&quot;http://url.feed.com&quot;); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument X = new XmlDocument(); X.Load(xtr); if (!(X == null)) { try { XmlNodeList ArticleList = X.SelectNodes(&quot;InfoStreamResults/Article&quot;); foreach(XmlNode Article in ArticleList) { try { XmlNodeList CatNodesList = Article.SelectNodes(&quot;Categories/Category&quot;); foreach( XmlNode category in CatNodesList) { if (Article.SelectSingleNode(&quot;Category&quot;) != null) { categoryName = EncodeIt(Article.SelectSingleNode(&quot;Category&quot;).Value); Response.Write(categoryName); Response.Write(&quot;&lt;br /&gt;&quot;); } } } catch (Exception ex) { Response.Write(ex); Response.Write(&quot;&lt;br /&gt;&quot;); } } } catch (Exception ex) { Response.Write(ex); } }</pre> <p>In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought&nbsp;that now we are in another foreach loop, it would become&nbsp;&nbsp;category.SelectSingleNode(&quot;category<span class="st">&quot;). But I tried that and that didn't work either.</span></p> <p><span class="st">&nbsp;Incidentally, a sample of my XML looks like this;</span></p> <p><span class="st">&nbsp;&nbsp;</p> <pre class="prettyprint"> &lt;<span class="tag">InfoStreamResults</span><span class="attr"> Copyright=</span><span class="attrv">&quot;(c) 2006 xxxx.&quot;</span><span class="attr"> ArticleCount=</span><span class="attrv">&quot;1&quot;</span>&gt; &lt;<span class="tag">Article</span><span class="attr"> Created=</span><span class="attrv">&quot;12:05:00&quot;</span><span class="attr"> ID=</span><span class="attrv">&quot;1111&quot;</span>&gt; &lt;<span class="tag">Heading</span>&gt;heading title&lt;/<span class="tag">Heading</span>&gt; &lt;<span class="tag">Date</span>&gt;11/11/1111&lt;/<span class="tag">Date</span>&gt; &lt;<span class="tag">Contents</span>&gt; <span class="ec">![CDATA[</span> blah blah <span class="ec">]]</span>&gt; &lt;/<span class="tag">Contents</span>&gt; &lt;<span class="tag">Categories</span>&gt; &lt;<span class="tag">Category</span><span class="attr"> ID=</span><span class="attrv">&quot;11111&quot;</span>&gt;cat 1&lt;/<span class="tag">Category</span>&gt; &lt;<span class="tag">Category</span><span class="attr"> ID=</span><span class="attrv">&quot;11112&quot;</span>&gt;cat 2&lt;/<span class="tag">Category</span>&gt; &lt;/<span class="tag">Categories</span>&gt; &lt;/<span class="tag">Article</span>&gt; &lt;/<span class="tag">InfoStreamResults</span>&gt;</pre> <p>&nbsp;</p> <p></span>You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.</p> 2007-04-12T20:17:34-04:001663516http://forums.asp.net/p/1097714/1663516.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:&nbsp;</p> <pre class="prettyprint">XmlTextReader xtr = new XmlTextReader(&quot;http://url.feed.com&quot;); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument X = new XmlDocument(); X.Load(xtr); if (!(X == null)) { try { XmlNodeList ArticleList = X.SelectNodes(&quot;InfoStreamResults/Article&quot;); foreach(XmlNode Article in ArticleList) { try { XmlNodeList CatNodesList = Article.SelectNodes(&quot;Categories/Category&quot;); foreach( XmlNode category in CatNodesList) { if (Article.SelectSingleNode(&quot;Category&quot;) != null) { categoryName = EncodeIt(Article.SelectSingleNode(&quot;Category&quot;).Value); Response.Write(categoryName); Response.Write(&quot;&lt;br /&gt;&quot;); } } } catch (Exception ex) { Response.Write(ex); Response.Write(&quot;&lt;br /&gt;&quot;); } } } catch (Exception ex) { Response.Write(ex); } }</pre> <p>In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought&nbsp;that now we are in another foreach loop, it would become&nbsp;&nbsp;category.SelectSingleNode(&quot;category<span class="st">&quot;). But I tried that and that didn't work either.</span></p> <p><span class="st">&nbsp;Incidentally, a sample of my XML looks like this;</span></p> <p><span class="st">&nbsp;&nbsp;</p> <pre class="prettyprint"> &lt;<span class="tag">InfoStreamResults</span><span class="attr"> Copyright=</span><span class="attrv">&quot;(c) 2006 xxxx.&quot;</span><span class="attr"> ArticleCount=</span><span class="attrv">&quot;1&quot;</span>&gt; &lt;<span class="tag">Article</span><span class="attr"> Created=</span><span class="attrv">&quot;12:05:00&quot;</span><span class="attr"> ID=</span><span class="attrv">&quot;1111&quot;</span>&gt; &lt;<span class="tag">Heading</span>&gt;heading title&lt;/<span class="tag">Heading</span>&gt; &lt;<span class="tag">Date</span>&gt;11/11/1111&lt;/<span class="tag">Date</span>&gt; &lt;<span class="tag">Contents</span>&gt; <span class="ec">![CDATA[</span> blah blah <span class="ec">]]</span>&gt; &lt;/<span class="tag">Contents</span>&gt; &lt;<span class="tag">Categories</span>&gt; &lt;<span class="tag">Category</span><span class="attr"> ID=</span><span class="attrv">&quot;11111&quot;</span>&gt;cat 1&lt;/<span class="tag">Category</span>&gt; &lt;<span class="tag">Category</span><span class="attr"> ID=</span><span class="attrv">&quot;11112&quot;</span>&gt;cat 2&lt;/<span class="tag">Category</span>&gt; &lt;/<span class="tag">Categories</span>&gt; &lt;/<span class="tag">Article</span>&gt; &lt;/<span class="tag">InfoStreamResults</span>&gt;</pre> <p>&nbsp;</p> <p></span>You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.</p> 2007-04-12T20:17:36-04:001663519http://forums.asp.net/p/1097714/1663519.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# Oops, double post, sorry. 2007-04-12T20:19:25-04:001663540http://forums.asp.net/p/1097714/1663540.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Below is your code</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(X == null))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlNodeList ArticleList = X.SelectNodes(&quot;InfoStreamResults/Article&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach(XmlNode Article in ArticleList) <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlNodeList CatNodesList = Article.SelectNodes(&quot;Categories/Category&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(&quot;&lt;br /&gt;&quot;);<br> &nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br> &nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch (Exception ex)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(ex);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>HC</p> <p>-----</p> <p>Mark it as answered if it helped you</p> 2007-04-12T20:32:18-04:001667173http://forums.asp.net/p/1097714/1667173.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# Brilliant, thanks&nbsp; - that's worked. 2007-04-16T10:21:16-04:002476897http://forums.asp.net/p/1097714/2476897.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Hi,</p> <p>&nbsp;I am having a very similar problem, except it's slightly more complicated. I need to check the attribute value before deciding what to do with the inner text. I am converting a large xml file (100,000 lines) to a csv file, except i'm struggling with the custom attributes section - see below:</p> <p>&nbsp;<pre class="prettyprint">&lt;product product-id=&quot;048002347&quot;&gt; &lt;display-name&gt;Cotton crew-neck printed T-shirt&lt;/display-name&gt; &lt;long-description&gt;Superdry cotton short-sleeved T-shirt with Osaka 6 print emblazoned across chest.&lt;/long-description&gt; &lt;online-flag&gt;false&lt;/online-flag&gt; &lt;available-flag&gt;true&lt;/available-flag&gt; &lt;searchable-flag&gt;true&lt;/searchable-flag&gt; &lt;tax-class-id&gt;default&lt;/tax-class-id&gt; &lt;brand&gt;Superdry&lt;/brand&gt; &lt;manufacturer-name&gt;Superdry&lt;/manufacturer-name&gt; &lt;custom-attributes&gt; &lt;custom-attribute attribute-id=&quot;Calendar Event&quot;&gt;N/A&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Care Instructions&quot;&gt;Machine Wash 30°C&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Colour&quot;&gt;Black&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Cuff Type&quot;&gt;N/A&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Exclusive&quot;&gt;N/A&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Fabric&quot;&gt;Cotton&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Fair Trade&quot;&gt;N/A&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Gender&quot;&gt;Men&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Guarantee&quot;&gt;N/A&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;House of Fraser Magazine&quot;&gt;No&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Organic&quot;&gt;No&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Parent Colour&quot;&gt;Black&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Parent Style&quot;&gt;TOPS&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Range&quot;&gt;PRE-SEASON/TRANSITIONAL&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Recycled&quot;&gt;N/A&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Size&quot;&gt;XL&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Style&quot;&gt;TOP- T-SHIRT&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Television Line&quot;&gt;No&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;Web Supply Lane&quot;&gt;iForce Deliveries&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;qosListID&quot;&gt;QOS1&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;sellByUnit&quot;&gt;false&lt;/custom-attribute&gt; &lt;custom-attribute attribute-id=&quot;COMPOSITION&quot;&gt;100% Cotton&lt;/custom-attribute&gt; &lt;/custom-attributes&gt; &lt;/product&gt;</pre> <P>What I need to do is first check for the attribute-id value, i.e. </P><pre class="prettyprint">&nbsp;<pre class="prettyprint">XmlNodeList attributeList = n.SelectNodes(<SPAN class=st>"custom-attributes/custom-attribute"</SPAN>); <SPAN class=kwd>string</SPAN> calendar = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>foreach</SPAN> (XmlNode a <SPAN class=kwd>in</SPAN> attributeList) { <SPAN class=kwd>if</SPAN> (a.Attributes[<SPAN class=st>"attribute-id"</SPAN>].Value == <SPAN class=st>"Calendar Event"</SPAN>) { calendar = a.InnerText; } } </pre>&nbsp;</PRE> <P>And then assign it the inner value if it exists, else leave the string as null. Now all that's happening is the string is returned as nul every single time.</P> <P>I am using a textwriter to write the data to a textfile, so a shortened version of the code (with everything required for this purpose is below)</P><pre class="prettyprint">&nbsp;<pre class="prettyprint"><SPAN class=kwd>int</SPAN> id = 0; <SPAN class=kwd>string</SPAN> CSVFile = <SPAN class=st>"HOFProds.txt"</SPAN>; TextWriter tw = <SPAN class=kwd>new</SPAN> StreamWriter(CSVFile); tw.WriteLine(<SPAN class=st>"id|product-id|ean|display-name|long-description|online-flag|available-flag|"</SPAN> + <SPAN class=st>"searchable-flag|tax-class-id|brand|manufacturer-name|Calendar Event|Care Instructions|Colour|"</SPAN> + <SPAN class=st>"Cuff Type|Exclusive|Fabric|Fair Trade|Gender|Guarantee|House of Fraser Magazine|Organic|Parent Colour|"</SPAN> + <SPAN class=st>"Parent Style|Range|Recycled|Size|Style|Television Line|Web Supply Lane|qosListID|sellByUnit|COMPOSITION|"</SPAN> + <SPAN class=st>"PrimaryImage|SecondaryImage1|SecondaryImage2|Sizing|Finish|Material|Skin Type|Animal Tested|"</SPAN> + <SPAN class=st>"WEIGHT|UnitMeasure|UnitQty|UnitQtyPrd|SecondaryImage3|SecondaryImage4|SecondaryImage5"</SPAN>); XmlDocument hofProds = <SPAN class=kwd>new</SPAN> XmlDocument(); hofProds.Load(<SPAN class=st>"StagingHofCatalog.xml"</SPAN>); XmlNodeList productList = <SPAN class=kwd>null</SPAN>; productList = hofProds.GetElementsByTagName(<SPAN class=st>"product"</SPAN>); <SPAN class=kwd>foreach</SPAN> (XmlNode n <SPAN class=kwd>in</SPAN> productList) { id = id + 1; <SPAN class=kwd>string</SPAN> ean = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> displayName = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> longDescription = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> onlineFlag = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> availableFlag = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> searchableFlag = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> taxClass = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> brand = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> manufacturerName = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"ean"</SPAN>] != <SPAN class=kwd>null</SPAN>) { ean = n[<SPAN class=st>"ean"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"display-name"</SPAN>] != <SPAN class=kwd>null</SPAN>) { displayName = n[<SPAN class=st>"display-name"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"long-description"</SPAN>] != <SPAN class=kwd>null</SPAN>) { longDescription = n[<SPAN class=st>"long-description"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"online-flag"</SPAN>] != <SPAN class=kwd>null</SPAN>) { onlineFlag = n[<SPAN class=st>"online-flag"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"available-flag"</SPAN>] != <SPAN class=kwd>null</SPAN>) { availableFlag = n[<SPAN class=st>"available-flag"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"searchable-flag"</SPAN>] != <SPAN class=kwd>null</SPAN>) { searchableFlag = n[<SPAN class=st>"searchable-flag"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"tax-class-id"</SPAN>] != <SPAN class=kwd>null</SPAN>) { taxClass = n[<SPAN class=st>"tax-class-id"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"brand"</SPAN>] != <SPAN class=kwd>null</SPAN>) { brand = n[<SPAN class=st>"brand"</SPAN>].InnerText; } <SPAN class=kwd>if</SPAN> (n[<SPAN class=st>"manufacturer-name"</SPAN>] != <SPAN class=kwd>null</SPAN>) { manufacturerName = n[<SPAN class=st>"manufacturer-name"</SPAN>].InnerText; } <SPAN class=kwd>string</SPAN> calendar = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> care = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> colour = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> cuff = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> exclusive = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> fabric = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> fairtrade = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> gender = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> guarantee = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> hofmagazine = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> organic = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> parentColour = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> parentStyle = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> range = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> recycled = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> size = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> style = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> tvLine = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> webSupplyLane = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> qosListID = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> sellByUnit = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> composition = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> primaryImage = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> SecondaryImage1 = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> SecondaryImage2 = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> Sizing = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> Finish = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> Material = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> SkinType = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> AnimalTested = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> WEIGHT = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> UnitMeasure = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> UnitQty = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> UnitQtyPrd = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> SecondaryImage3 = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> SecondaryImage4 = <SPAN class=st>"null"</SPAN>; <SPAN class=kwd>string</SPAN> SecondaryImage5 = <SPAN class=st>"null"</SPAN>; XmlNodeList attributeList = n.SelectNodes(<SPAN class=st>"custom-attributes/custom-attribute"</SPAN>); <SPAN class=kwd>foreach</SPAN> (XmlNode a <SPAN class=kwd>in</SPAN> attributeList) { <SPAN class=kwd>if</SPAN> (a.Attributes[<SPAN class=st>"attribute-id"</SPAN>].Value == <SPAN class=st>"Calendar Event"</SPAN>) { calendar = a.InnerText; } } tw.WriteLine(id + <SPAN class=st>"|"</SPAN> + n.Attributes[<SPAN class=st>"product-id"</SPAN>].Value + <SPAN class=st>"|"</SPAN> + ean + <SPAN class=st>"|"</SPAN> + displayName + <SPAN class=st>"|"</SPAN> + longDescription + <SPAN class=st>"|"</SPAN> + onlineFlag + <SPAN class=st>"|"</SPAN> + availableFlag + <SPAN class=st>"|"</SPAN> + searchableFlag + <SPAN class=st>"|"</SPAN> + taxClass + <SPAN class=st>"|"</SPAN> + brand + <SPAN class=st>"|"</SPAN> + manufacturerName + <SPAN class=st>"|"</SPAN> + calendar + <SPAN class=st>"|"</SPAN> + care + <SPAN class=st>"|"</SPAN> + colour + <SPAN class=st>"|"</SPAN> + cuff + <SPAN class=st>"|"</SPAN> + exclusive + <SPAN class=st>"|"</SPAN> + fabric + <SPAN class=st>"|"</SPAN> + fairtrade + <SPAN class=st>"|"</SPAN> + gender + <SPAN class=st>"|"</SPAN> + guarantee + <SPAN class=st>"|"</SPAN> + hofmagazine + <SPAN class=st>"|"</SPAN> + organic + <SPAN class=st>"|"</SPAN> + parentColour + <SPAN class=st>"|"</SPAN> + parentStyle + <SPAN class=st>"|"</SPAN> + range + <SPAN class=st>"|"</SPAN> + recycled + <SPAN class=st>"|"</SPAN> + size + <SPAN class=st>"|"</SPAN> + style + <SPAN class=st>"|"</SPAN> + tvLine + <SPAN class=st>"|"</SPAN> + webSupplyLane + <SPAN class=st>"|"</SPAN> + qosListID + <SPAN class=st>"|"</SPAN> + sellByUnit + <SPAN class=st>"|"</SPAN> + composition + <SPAN class=st>"|"</SPAN> + primaryImage + <SPAN class=st>"|"</SPAN> + SecondaryImage1 + <SPAN class=st>"|"</SPAN> + SecondaryImage2 + <SPAN class=st>"|"</SPAN> + Sizing + <SPAN class=st>"|"</SPAN> + Finish + <SPAN class=st>"|"</SPAN> + Material + <SPAN class=st>"|"</SPAN> + SkinType + <SPAN class=st>"|"</SPAN> + AnimalTested + <SPAN class=st>"|"</SPAN> + WEIGHT + <SPAN class=st>"|"</SPAN> + UnitMeasure + <SPAN class=st>"|"</SPAN> + UnitQty + <SPAN class=st>"|"</SPAN> + UnitQtyPrd + <SPAN class=st>"|"</SPAN> + SecondaryImage3 + <SPAN class=st>"|"</SPAN> + SecondaryImage4 + <SPAN class=st>"|"</SPAN> + SecondaryImage5); } tw.Close(); </pre>&nbsp;</PRE> </p> <p>&nbsp;Just to clarify that before we get to the custom-attributes section, the program works fine because the data is output correctly. It's only when I get to analyzing the custom-attributes section that does the data become construed.</p> <p>I have tried to use a separate node list and insert the attributes that way except this doesn't have no binding or sequence then as sometimes the attributes may not exist, this is why i have to allow for them all and check them, if they are there&nbsp;then insert them, if not then insert null.</p> <p>&nbsp;Hopefully I have been clear, </p> <p>Many thanks for any help in advance,</p> <p>ChanandCo</p> 2008-07-09T10:50:15-04:002625970http://forums.asp.net/p/1097714/2625970.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p><strong>Haissam over a year ago you made this one.</strong></p> <p>&nbsp;&nbsp;&nbsp;&nbsp; if (!(X == null))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlNodeList ArticleList = X.SelectNodes(&quot;InfoStreamResults/Article&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach(XmlNode Article in ArticleList) <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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;&nbsp; } <br> </p> <p><strong></strong>&nbsp;</p> <p><strong>I'm curious to know how would you accomplish same results using LINQ to XML?</strong></p> <p>&nbsp;</p> <p>-Anargeek77</p> 2008-09-17T03:26:42-04:002626323http://forums.asp.net/p/1097714/2626323.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Hey, you would need to convert your XMLDocument to an XDocument/Xelement to be able to use it with LINQ.</p> <pre class="prettyprint">XElement elements = XElement.Load(pathToFile); var ranges = from r in elements.DescendantsAndSelf(&quot;range&quot;) select r; foreach (var x in ranges) { //do your work here. }</pre>&nbsp;HTH's :) <p>&nbsp;</p> 2008-09-17T07:07:26-04:002769430http://forums.asp.net/p/1097714/2769430.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>HI Hassam, </p> <p>&nbsp;</p> You wrote the code above to get the category.innerxml out but what if I wanted to get the text and the Id number out. for example <font color="#0000ff" size="2"><font color="#0000ff" size="2"></font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> <p>&lt;</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">Category</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">ID</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">430009730</font></font><font size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&gt;</font></font><font size="2">International affairs</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&lt;/</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">Category</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&gt; </font></font></p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2">how would i get the id as well as the international affairs out. </font></font></p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2"></font></font>&nbsp;</p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2">Please help or anyone please help </font></font></p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2"></font></font>&nbsp;</p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2">Thanks, </font></font></p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2"></font></font>&nbsp;</p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2">R8VI</p> </font></font> 2008-11-26T13:24:29-05:003927842http://forums.asp.net/p/1097714/3927842.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>You can select the attribute.&nbsp;</p> <p>Please see the following example:-</p> <p><a href="http://www.csharp-examples.net/xml-nodes-by-attribute-value/">http://www.csharp-examples.net/xml-nodes-by-attribute-value/</a></p> <p><br> </p> 2010-06-15T12:42:41-04:004695001http://forums.asp.net/p/1097714/4695001.aspx/1?Re+Loop+through+sub+XmlNodeList+with+C+Re: Loop through sub XmlNodeList with C# <p>Your solution worked for me <img src="../../../../scripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0"></p> <p>Cheers,</p> <p>Sudha</p> 2011-11-22T08:30:38-05:00