473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Xml.net for an XML person

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.

Nov 15 '05 #1
3 1075
suzy <me@nospam.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2


suzy wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 15 '05 #3
Kirk has a good write up on XPathDocument and XSLT [0].

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[0] http://blogs.xmladvice.com/kaevans/a...01/03/235.aspx

"suzy" <me@nospam.com> wrote in message
news:OW*************@tk2msftngp13.phx.gbl...
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.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Steve Bywaters | last post by:
There are several hundred of my client's franchisees happily accessing my ASP/VBscript web site... but one - just one - has reported the following: "I have been trying to log onto the LSM since...
2
by: Remco Groot Beumer | last post by:
Hello I've created an ASP.NET page, which uses some variables which are set after login. When person A logs in, a new public object is instanced (i'm not sure if that is a correct word), after...
5
by: Jason | last post by:
I am running Exchange 2003 on a Windows 2003 Active Directory. I am trying to mailbox-enable a user account with VB.NET. Dim person As CDO.Person Dim mailbox As CDOEXM.IMailboxStore person =...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
1
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
3
by: mProgramz | last post by:
//Specification: This program plays a version of //the card game of 21. //A human player is pitted against the computer. //The player who is the closest to 21 without //going over wins the hand. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.