0
votes

I have to read the XML:

<items>
    <item>
      <prop1>value1</prop1>
      <prop2>value2</prop2>
      <prop3>value3</prop3>
    </item>
    <item>
      <prop1>value1</prop1>
      <prop2>value2</prop2>
      <prop3>value3</prop3>
    </item>
</items>

And put the values into a List<CLASS>.

Some options:

  • Use XMLSerializer to deserialize to a List
  • Use XMLDocument to read each item using SelectNodes with XPath and put the values into a List
  • Use XMLReader to read each node and put the values into a List
  • Other option...
3
Consider carefully how fast it needs to be. There's generally a trade-off between optimization and ease of maintenance. If you profile it and find it's a bottleneck, you could always try the different methods (don't forget LINQ, as Mr. Diplo mentioned) and benchmark them. - TrueWill
Pre-optimization is the root of all evil =D LINQ it man! - Jader Dias
Seriously now, I used always used XSD.exe, it's a great tool! - Jader Dias

3 Answers

6
votes

By far the fastest that I have seen is to use XSD.exe to create an XSD and Class to go with it, then use serialization.

1
votes

Another option would be to use LinqToXml.

-1
votes

If you're in dotnet, install the WCF starter pack. Then you'll have an option "Paste XML as Types", so you can cut the XML you're looking to serialize into the clipboard and paste it into code as a serializable type. Then you can just serialize the XML and get the values through the class.