tags:

views:

33741

answers:

8

The default methods for dealing with xml in c# seem incredibly crude to me, leading me to suspect that I must be missing something in my searches. What is considered the standard best practices to parse xml files in c#?

+25  A: 

I'd use LINQ to XML if you're in .NET 3.5.

Jon Galloway
Here is an updated link for VS 2010 as the above is brokenhttp://msdn.microsoft.com/en-us/library/bb387098.aspx
Toymakerii
Thanks, updated
Jon Galloway
+3  A: 

I'm not sure whether "best practice for parsing xml" exists. There are numerous technologies suited for different situations. Which way to use depends on concrete scenario.

You can go with XLinq, XmlReader, XPathNavigator or even regular expressions. If you elaborate your needs I can try to give some suggestions.

aku
+1  A: 

What's wrong with XmlTextReader, XmlReader, XmlNodeReader and the System.Xml.XPath namespace? (XPathNavigator, XPathDocument, XPathExpression, XPathnodeIterator)?

Usually XPath makes reading XML easier, which is what you might be looking for.

Vinko Vrsalovic
What's wrong with them? Everything. It's a step above grovelling through the raw xml by hand, but only just.
Wedge
I disagree, especially regarding XPath
Vinko Vrsalovic
OK, well maybe XPath is an exception. Still, using the basic APIs is a lot better than stabbing yourself in the eye with a pointy stick, but not by much. For small, simple xml files (which some may say all xml files should be), they're almost more trouble than they're worth.
Wedge
Providing arguments instead of analogies might work better.
Vinko Vrsalovic
A: 

If you're .NET 2.0, try XmlReader and it's subclasses XmlTextReader, XmlValidatingReader provide a fast, lightweight (memory usage etc), forward only way to parse an XML file.

If you need XPath capabilities try the XPathNavigator. If you need the entire document in memory try XmlDocument.

Ash
+7  A: 

it's very simple, I know this is standard methods but you can create your own library to deal with that much better. Here is some examples:

XmlDocument xmlDoc= new XmlDocument(); //* create an xml document object.
xmlDoc.Load("yourXMLFile.xml"); //* load the XML document from the specified file.

//* Get elements.
XmlNodeList girlAddress = xmlDoc.GetElementsByTagName("gAddress");
XmlNodeList girlAge = xmlDoc.GetElementsByTagName("gAge"); 
XmlNodeList girlCellPhoneNumber = xmlDoc.GetElementsByTagName("gPhone");

//* Display the results.
Console.WriteLine("Address: " + girlAddress[0].InnerText);
Console.WriteLine("Age: " + girlAge[0].InnerText);
Console.WriteLine("Phone Number: " + girlCellPhoneNumber[0].InnerText);

Also there are some other methods to work with. f.ex here. And I think there are no one best method to do this, you always need to choose it by your self, what is most suitable for you.

Lukas Šalkauskas
+1 for mentioning XmlDocument, which is much more convenient than serialisation interfaces in some cases. If you are after one specific element, you can access child elements with the indexer: xmlDoc["Root"], and these can be chained: xmlDoc["Root"]["Folder"]["Item"] to dig down the hierarchy (although it's sensible to validate that these elements actually exist)
Jason Williams
+10  A: 

Use a good XSD Schema to create a set of classes with xsd.exe and use an XmlSerializer to create a object tree out of your XML and vice versa. If you have few restrictions on your model, you could even try to create a direct mapping between you model classes and the XML with the Xml*Attributes.

There is an introductory article about XML Serialisation on MSDN.

Performance tip: Constructing an XmlSerializer is expensive. Keep a reference to your XmlSerializer instance if you intend to parse/write multiple XML files.

David Schmitt
David, I've always preferred this way... Unfortuantely, Xsd.exe screws up the class names which I've to go and modify by hand. This, of course, leaves my classes no longer 'auto generated'... Any suggestions for forcing XSD to customize tag names during generation? Or perhaps another tool to run?
Vyas Bharghava
See http://www.codeproject.com/KB/cs/xsdtidy.aspx and http://blog.dotnetwiki.org/XsdTidyXSDMappingBeautifier.aspx
David Schmitt
A: 

If you're processing a large amount of data (many megabytes) then you want to be using XmlReader to stream parse the XML. Anything else (XPathNavigator, XElement, XmlDocument and even XmlSerializer if you keep the full generated object graph) will result in high memory usage and also a very slow load time. Of course, if you need all the data in memory anyway, then you may not have much choice.

Simon Steele
A: 

The DataSet.ReadXml method reads XML schema and data into the DataSet using the specified file. This link has a sample w3mentor c# xml parsing