Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get all the subnodes of a node with certain attribute?
    text
    copied!<p>I have an xml document that goes like this:</p> <pre><code>&lt;Menu&gt; &lt;Category name="Comida Rapida"&gt; &lt;Food cocina="si"&gt; &lt;Name&gt;Haburguesa&lt;/Name&gt; &lt;Price&gt;10&lt;/Price&gt; &lt;/Food&gt; &lt;Food&gt; &lt;Name&gt;Papas Fritas&lt;/Name&gt; &lt;Price&gt;20&lt;/Price&gt; &lt;/Food&gt; &lt;/Category&gt; &lt;Category name="Bebidas"&gt; &lt;Food&gt; &lt;Name&gt;Pepsi&lt;/Name&gt; &lt;Price&gt;30&lt;/Price&gt; &lt;/Food&gt; &lt;Food cocina="si"&gt; &lt;Name&gt;Coca Cola&lt;/Name&gt; &lt;Price&gt;40&lt;/Price&gt; &lt;/Food&gt; &lt;/Category&gt; &lt;/Menu&gt; </code></pre> <p>What I want to do is go through each <code>&lt;Category&gt;</code> checking if the attribute is what I need, for example "Bebidas", so the part I'm interested in is:</p> <pre><code>&lt;Food&gt; &lt;Name&gt;Pepsi&lt;/Name&gt; &lt;Price&gt;30&lt;/Price&gt; &lt;/Food&gt; &lt;Food cocina="si"&gt; &lt;Name&gt;Coca Cola&lt;/Name&gt; &lt;Price&gt;40&lt;/Price&gt; &lt;/Food&gt; </code></pre> <p>Now that I have this I want to do something similar to what I have done already:</p> <p>First I want to print out all:</p> <pre><code>Pepsi 30 Coca Cola 40 </code></pre> <p>And the I want to print out only the ones that food had the attribute <code>cocina="si"</code>, so:</p> <pre><code>Coca Cola 40 </code></pre> <p>So I have various questions:</p> <p>First of all which approach to use, I am confused by the abundance of possible methods and implementations: XmlDocument, XmlReader, XmlTextReader, etc.</p> <p>From <a href="https://stackoverflow.com/questions/1505075/deciding-on-when-to-use-xmldocument-vs-xmlreader">this question</a> I gather XmlDocument is the easier to use, that would be great, the simpler, the better as I am quite new to parsing Xml files as you can appreciate.</p> <p>Now to the actual implementation, I have tried all sort of things with not much succes, I seem to be able to do some parts but not all together.</p> <pre><code>XmlNodeList elemList = doc.GetElementsByTagName("Category"); for (int i = 0; i &lt; elemList.Count; i++) { Console.WriteLine(elemList[i].InnerXml); } </code></pre> <p>This will output:</p> <pre><code>&lt;Food&gt;&lt;Name&gt;Haburguesa&lt;/Name&gt;&lt;Price&gt;10&lt;/Price&gt;&lt;/Food&gt;&lt;Food&gt;&lt;Name&gt;Papas Fritas&lt;/Name&gt;&lt;Price&gt;20&lt;/Price&gt;&lt;/Food&gt; &lt;Food&gt;&lt;Name&gt;Pepsi&lt;/Name&gt;&lt;Price&gt;30&lt;/Price&gt;&lt;/Food&gt;&lt;Food&gt;&lt;Name&gt;Coca Cola&lt;/Name&gt;&lt;Price&gt;40&lt;/Price&gt;&lt;/Food&gt; </code></pre> <p>Which makes sense but now, how do I check if the category has the attribute <code>name="cocina"</code>?</p> <p>I'm guessing something like this could help:</p> <pre><code>for (int j = 0; j &lt; elemList[i].Attributes.Count; j++) { //?? } </code></pre> <p>But I can't find something like <code>MoveToAttribute()</code> in XmlTextReader.</p> <p>And then again, how do I check whether has the attribute <code>cocina="si"</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload