How do I get all the subnodes of a node with certain attribute?

SirDouble

New Member
I have an xml document that goes like this:\[code\]<Menu> <Category name="Comida Rapida"> <Food cocina="si"> <Name>Haburguesa</Name> <Price>10</Price> </Food> <Food> <Name>Papas Fritas</Name> <Price>20</Price> </Food> </Category> <Category name="Bebidas"> <Food> <Name>Pepsi</Name> <Price>30</Price> </Food> <Food cocina="si"> <Name>Coca Cola</Name> <Price>40</Price> </Food> </Category></Menu>\[/code\]What I want to do is go through each \[code\]<Category>\[/code\] checking if the attribute is what I need, for example "Bebidas", so the part I'm interested in is:\[code\]<Food> <Name>Pepsi</Name> <Price>30</Price></Food><Food cocina="si"> <Name>Coca Cola</Name> <Price>40</Price></Food>\[/code\]Now that I have this I want to do something similar to what I have done already:First I want to print out all:\[code\]Pepsi 30Coca Cola 40\[/code\]And the I want to print out only the ones that food had the attribute \[code\]cocina="si"\[/code\], so:\[code\]Coca Cola 40\[/code\]So I have various questions:First of all which approach to use, I am confused by the abundance of possible methods and implementations: XmlDocument, XmlReader, XmlTextReader, etc.From this question 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.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.\[code\]XmlNodeList elemList = doc.GetElementsByTagName("Category");for (int i = 0; i < elemList.Count; i++){ Console.WriteLine(elemList.InnerXml);}\[/code\]This will output:\[code\]<Food><Name>Haburguesa</Name><Price>10</Price></Food><Food><Name>Papas Fritas</Name><Price>20</Price></Food><Food><Name>Pepsi</Name><Price>30</Price></Food><Food><Name>Coca Cola</Name><Price>40</Price></Food> \[/code\]Which makes sense but now, how do I check if the category has the attribute \[code\]name="cocina"\[/code\]?I'm guessing something like this could help:\[code\]for (int j = 0; j < elemList.Attributes.Count; j++){ //?? }\[/code\]But I can't find something like \[code\]MoveToAttribute()\[/code\] in XmlTextReader.And then again, how do I check whether has the attribute \[code\]cocina="si"\[/code\]?
 
Back
Top