XML parsing

  • Thread starter Thread starter DotNetNewbie
  • Start date Start date
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?
 
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.
 
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!
 
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.
 
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
 
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
 
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
 
Back
Top