0

I'm playing with an imported XML file and using XMLDocument I'm wondering if there is a better way to do the same thing. Basically root contains MHZ nodes and each MHZ contains several devices and one name. I want to count every MHZ nodes and display the number of devices in each MHZ :

        String xmlName = "tts.xml";
        XmlDocument readDoc = new XmlDocument();
        readDoc.Load(xmlName);
        int fileNb = readDoc.SelectNodes("//MHZ").Count;
        Console.WriteLine("MHZ number : "+fileNb);
        for (int i = 0; i < fileNb; i++)
        {   
            int deviceNb = readDoc.SelectNodes("//MHZ[" +(i+1)+ "]/device").Count;
            Console.WriteLine(deviceNb);
        }
xenom
  • 377
  • 1
  • 5
  • 15
  • You could use the [serializer](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.deserialize(v=vs.71).aspx) to turn your XML into a more natural C# object. – RichardTowers Jul 03 '12 at 09:07
  • @xenom: it will be good if you can provide xml file structure to get exact solution.. as you are saying that MHZ contains devices and name.. so 'name' is attribute or element.. so please provide xml – Dr. Rajesh Rolen Jul 03 '12 at 09:24

5 Answers5

1

If you're using .NET 3.5 or later, I'd use LINQ to XML:

var doc = XDocument.Load(xmlName);
var mhzs = doc.Descendants("MHZ");
Console.WriteLine("Count of MHZ: {0}", mhz.Count());
foreach (var mhz in mhzs)
{
    Console.WriteLine(mhz.Elements("device").Count());
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • And how would you do to write the name of each MHZ ? `Console.WriteLine(mhz.Elements("name"));` ? – xenom Jul 03 '12 at 09:19
  • 1
    should be compile time error in this code...in Console.WriteLine("Count of MHZ: {0}", mhz.Count()); it should be 'mhzs.Count()'.... missing 's' from mhzs – Dr. Rajesh Rolen Jul 03 '12 at 09:27
0

Have you had a look at Linq to XML for more info check here http://msdn.microsoft.com/en-us/library/bb387098.aspx

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
0

Create serializable objects and then deserialize your xml into a list of the objects. This way you can stay true to OOP.

[DataContract]
public class Device
{
   [DataMember]
   public string Property { get; set; }
}

Or look at: DataContract XML serialization and XML attributes or How to Deserialize XML document

Community
  • 1
  • 1
Kolky
  • 2,917
  • 1
  • 21
  • 42
  • I was thinking about serialization at the beginning, but I only need to browse and print few infos. I find that a bit complicated. However if I need more operations I'll do that. – xenom Jul 03 '12 at 09:17
0

In some cases I create object(s) that represents the structure of the XML and deserialize the XML to these object(s) This gives me a much better way of looking at the data, validating and iterating through collections of data.

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
0

XMLDocument is easy to use as your are already using it. Also it's worth to look at XPath

thelinh bui
  • 226
  • 6
  • 14