Xml.net for an XML person

  • Thread starter Thread starter suzy
  • Start date Start date
S

suzy

Hello,

If I have some XML in the following format:

<Foods>
<Food Name="Pizza">
<Ingredient Amount="500g">Flour</Ingredient>
<Ingredient Amount="100g">Cheese</Ingredient>
</Food>
<Food Name="Pasta">
<Ingredient Amount="500g">Pasta</Ingredient>
<Ingredient Amount="50g">Tomato Puree</Ingredient>
</Food>
</Foods>

I want to search the XML doc for a certain type of "Food", then read the
ingredient values for that type of food. I do not need to write/amend the
XML doc. What is the best/efficient way to do this?

If I was using normal XML I would have just loaded the XML into a
DomDocument and used XPath to find the node, assign it to a node object then
use .SelectSingleNode, etc. But I have read that using an DomDocument
object in c# is inefficient if you are just reading (and not writing) XML.

Any example or links would be appreciated.

Thanks.
 
suzy said:
If I have some XML in the following format:

<Foods>
<Food Name="Pizza">
<Ingredient Amount="500g">Flour</Ingredient>
<Ingredient Amount="100g">Cheese</Ingredient>
</Food>
<Food Name="Pasta">
<Ingredient Amount="500g">Pasta</Ingredient>
<Ingredient Amount="50g">Tomato Puree</Ingredient>
</Food>
</Foods>

I want to search the XML doc for a certain type of "Food", then read the
ingredient values for that type of food. I do not need to write/amend the
XML doc. What is the best/efficient way to do this?

If I was using normal XML I would have just loaded the XML into a
DomDocument and used XPath to find the node, assign it to a node object then
use .SelectSingleNode, etc. But I have read that using an DomDocument
object in c# is inefficient if you are just reading (and not writing) XML.

Any example or links would be appreciated.

Well, it's less efficient than using an XmlReader and stopping reading
when you've got all the info you want - but do you *really* care about
the efficiency here? Do you actually know that loading it into a
document and using XPath isn't efficient enough?

It's likely to be quick to write and easy to maintain.
 
suzy said:
If I have some XML in the following format:

<Foods>
<Food Name="Pizza">
<Ingredient Amount="500g">Flour</Ingredient>
<Ingredient Amount="100g">Cheese</Ingredient>
</Food>
<Food Name="Pasta">
<Ingredient Amount="500g">Pasta</Ingredient>
<Ingredient Amount="50g">Tomato Puree</Ingredient>
</Food>
</Foods>

I want to search the XML doc for a certain type of "Food", then read the
ingredient values for that type of food. I do not need to write/amend the
XML doc. What is the best/efficient way to do this?

If I was using normal XML I would have just loaded the XML into a
DomDocument and used XPath to find the node, assign it to a node object then
use .SelectSingleNode, etc. But I have read that using an DomDocument
object in c# is inefficient if you are just reading (and not writing) XML.

If you want to use XPath but don't need to modify anything in the
document then you could use a System.Xml.XPath.XPathDocument which
should be more efficient than using a System.Xml.XmlDocument.
 
Back
Top