Help with parsing some XML -- please help!

A

almurph

Hi,

I'm using XmlDocument but having problems to parse this:



<?xml version="1.0" encoding="utf-8" ?>
- <vehicles>
- <car>
<id>6</id>
<reg>4</reg>
<reg>9</reg>
<reg>8</reg>
<reg>11111</reg>
<reg>12</reg>
</car>
- <car>
<id>4</id>
<reg>4</reg>
<reg>6</reg>
<reg>7</reg>
<reg>9876</reg>
</car>
- <car>
<id>9</id>
<reg>1</reg>
<reg>2</reg>
<reg>4</reg>
</car>
</vehicles>


I need to extract the the reg for each id into a hashtable with the
key as the id and the value as reg like:

6 4
6 9
6 8
6 1111
6 12


etc. Can anyone pelase help. I would greatly appreciate any comments/
suggestions/code-sampels that you may be able to offer.

Thanks,
Al.
 
M

Martin Honnen

etc. Can anyone pelase help. I would greatly appreciate any comments/
suggestions/code-sampels that you may be able to offer.

Can you use LINQ to XML? Requires .NET 3.5.
 
A

almurph

Can you use LINQ to XML? Requires .NET 3.5.

No, has to be XML, will XMLdocument help - am having trouble with all
the "reg" values? Any help please???

Thanks,
Al.
 
M

Martin Honnen

No, has to be XML, will XMLdocument help - am having trouble with all
the "reg" values? Any help please???

LINQ to XML is working against XML input. For instance you can do

XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
Dictionary<int, List<int>> result =
doc.Root.Elements("car").ToDictionary(c =>
(int)c.Element("id"), c => c.Elements("reg").Select(r => (int)r).ToList());
foreach (int id in result.Keys)
{
Console.WriteLine("{0}:", id);
foreach (int reg in result[id])
{
Console.WriteLine("\t{0}", reg);
}
Console.WriteLine();
}

and then with your XML sample the output is

6:
4
9
8
11111
12

4:
4
6
7
9876

9:
1
2
4

You can use that with .NET 3.5 and it is the preferred API with .NET
3.5. Use XmlDocument only for earlier versions of .NET.
 
M

Martin Honnen

No, has to be XML, will XMLdocument help - am having trouble with all
the "reg" values? Any help please???

With XmlDocument and .NET 2.0 you can use

XmlDocument doc = new XmlDocument();
doc.Load(@"..\..\XMLFile1.xml");
XmlNodeList cars = doc.SelectNodes("vehicles/car");
Dictionary<int, List<int>> result = new Dictionary<int,
List<int>>(cars.Count);
foreach (XmlElement car in cars)
{
XmlNodeList regs = car.SelectNodes("reg");
List<int> regList = new List<int>(regs.Count);
foreach (XmlElement reg in regs)
{
regList.Add(XmlConvert.ToInt32(reg.InnerText));
}
result[XmlConvert.ToInt32(car["id"].InnerText)] = regList;
}
foreach (int id in result.Keys)
{
Console.WriteLine("{0}:", id);
foreach (int reg in result[id])
{
Console.WriteLine("\t{0}", reg);
}
Console.WriteLine();
}
 
A

almurph

No, has to be XML, will XMLdocument help - am having trouble with all
the "reg" values? Any help please???

LINQ to XML is working against XML input. For instance you can do

             XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
             Dictionary<int, List<int>> result =
                 doc.Root.Elements("car").ToDictionary(c =>
(int)c.Element("id"), c => c.Elements("reg").Select(r => (int)r).ToList());
             foreach (int id in result.Keys)
             {
                 Console.WriteLine("{0}:", id);
                 foreach (int reg in result[id])
                 {
                     Console.WriteLine("\t{0}", reg);
                 }
                 Console.WriteLine();
             }

and then with your XML sample the output is

6:
         4
         9
         8
         11111
         12

4:
         4
         6
         7
         9876

9:
         1
         2
         4

You can use that with .NET 3.5 and it is the preferred API with .NET
3.5. Use XmlDocument only for earlier versions of .NET.

Hi Martin,

I'm getting:

Error 1 'System.Xml.XmlDocument' does not contain a definition for
'Root' and no extension method 'Root' accepting a first argument of
type 'System.Xml.XmlDocument' could be found (are you missing a using
directive or an assembly reference?)

Any ideas? Or anything simpler?

Desperate,
Al.
 
M

Martin Honnen

XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
Dictionary<int, List<int>> result =
doc.Root.Elements("car").ToDictionary(c =>
(int)c.Element("id"), c => c.Elements("reg").Select(r => (int)r).ToList());
foreach (int id in result.Keys)
{
Console.WriteLine("{0}:", id);
foreach (int reg in result[id])
{
Console.WriteLine("\t{0}", reg);
}
Console.WriteLine();
}

and then with your XML sample the output is

6:
4
9
8
11111
12

4:
4
6
7
9876

9:
1
2
4

You can use that with .NET 3.5 and it is the preferred API with .NET
3.5. Use XmlDocument only for earlier versions of .NET.

I'm getting:

Error 1 'System.Xml.XmlDocument' does not contain a definition for
'Root' and no extension method 'Root' accepting a first argument of
type 'System.Xml.XmlDocument' could be found (are you missing a using
directive or an assembly reference?)

Use the code as posted, it uses XDocument, not XmlDocument. Not sure why
you change it to use XmlDocument and then complaint.
 

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