XML parsing

D

DotNetNewbie

Hello,


My XML looks like this:

<cars>
<mfg name="honda">
<car name="accord" />
<car name="civic" />
</mfg>
<mfg name="ford">
<car name="escort" />
<car name="taurus" />
</mfg>
</cars>

I want to get ONLY the cars for a specific mfg. example: get me all
the car nodes that are ford's.


What is the most effecient way of doing this?
 
J

Jon Skeet [C# MVP]

DotNetNewbie said:
My XML looks like this:

<cars>
<mfg name="honda">
<car name="accord" />
<car name="civic" />
</mfg>
<mfg name="ford">
<car name="escort" />
<car name="taurus" />
</mfg>
</cars>

I want to get ONLY the cars for a specific mfg. example: get me all
the car nodes that are ford's.


What is the most effecient way of doing this?

Do you really need the most *efficient* way of doing this rather than,
say, the most readable or maintainable? How big is your file going to
be in real life? Is it actually a file, or do you already have it in
memory?

Personally, I'd use LINQ to XML if .NET 3.5 is an option.
 
D

DotNetNewbie

Do you really need the most *efficient* way of doing this rather than,
say, the most readable or maintainable? How big is your file going to
be in real life? Is it actually a file, or do you already have it in
memory?

Personally, I'd use LINQ to XML if .NET 3.5 is an option.

--
Jon Skeet - <[email protected]>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk- Hide quoted text -

- Show quoted text -

I don't have the option of linq, it has to be done in at most asp.net
2.0 style :(

Is there a method in xmldocument that I can do this in? I'm stuck!
 
J

Jon Skeet [C# MVP]

DotNetNewbie said:
I don't have the option of linq, it has to be done in at most asp.net
2.0 style :(

Is there a method in xmldocument that I can do this in? I'm stuck!

XPath is probably your best bet, to be honest - use SelectNodes and
pass in an appropriate XPath expression.
 
D

DotNetNewbie

XPath is probably your best bet, to be honest - use SelectNodes and
pass in an appropriate XPath expression.

could you help me out with the code? i'm not too familiar with xpath
 
P

Peter Bromberg [C# MVP]

This should get you started, but really you need to learn how to do this on
your own or you are going to be spending all your time on newsgroups waiting
for answers:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Xpath
{
class Program
{
static void Main(string[] args)
{
string strxml="<cars><mfg name=\"honda\"><car name=\"accord\"
/><car name=\"civic\" /></mfg><mfg name=\"ford\"><car name=\"escort\" /><car
name=\"taurus\" /></mfg></cars>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strxml);
XmlNodeList nodes = doc.SelectNodes("cars/mfg[@name='honda']");
foreach (XmlNode nod in nodes)
Console.WriteLine(nod.InnerXml);
// <car name="accord" /><car name="civic" />
}
}
}
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
 
A

Arne Vajhøj

DotNetNewbie said:
could you help me out with the code? i'm not too familiar with xpath

Quick example:

using System;
using System.Xml;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\cars.xml");
XmlElement fordmfg =
(XmlElement)doc.SelectSingleNode("//cars/mfg[@name='ford']");
XmlNodeList fordcars = fordmfg.GetElementsByTagName("car");
foreach(XmlNode car in fordcars)
{
Console.WriteLine(car.Attributes.GetNamedItem("name").Value);
}
Console.ReadKey();
}
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top