Attempting to leverage the myriad classes found in the .NET Framework’s System.Xml namespace for use in your applications can be a daunting proposition. Although they work well, .NET’s XML classes are sufficiently different from what most developers are familiar with to make them difficult to use effectively. Here are five tips for using the .NET XML classes to their best effect.

Tip #1: Always use streams instead of opening files
When opening a document for reading, it’s tempting to just specify a file path and let XmlReader take care of opening the file for you. If you can, though, you’re better off using a stream object to open the file and building the reader from a stream instead. Streams are flexible, and you can do things with them that you can’t do as easily with a file URI, like open a second, different type of reader or a writer from the same stream. See the next tip for a concrete example of how that can be handy.

Tip #2: Validate on writing
Using the stock classes, it’s not possible to validate a document against a DTD or schema as you write it. For situations where you need to do that, try writing the document to a memory stream and then reading it from the stream using XmlValidatingReader, like this:
MemoryStream s = new MemoryStream();
XmlTextWriter wtr = new XmlTextWriter(s, System.Text.Encoding.ASCII);
//write XML here
s.Seek(0, SeekOrigin.Begin);
//open a validating reader
XmlValidatingReader rdr = new XmlValidatingReader(new XmlTextReader(s));
rdr.Schemas.Add(null, “C:\\somefile.xsd”);
rdr.ValidationType = ValidationType.Schema;
}catch {
//validation error

If you later need to write the validated document out to file, just use the MemoryStream’s WriteTo method to copy the stream’s contents to a FileStream:
FileStream f = new FileStream(“C:\\somefile.xml”);

Tip #3: Let unwanted elements fall through
When parsing a document using an XmlReader class, be careful not to write your code in such a way that it depends on the order of elements appearing in an XML document. That’ll cause problems down the road if the document ever changes format. Instead, use the Name or LocalName properties of the reader to check the name of the current element and make your parsing code independent of element order:
if (rdr.LocalName.Equals(“ElementYouWant”)
else if(rdr.LocalName.Equals(“AnotherElement”)

Using a parsing strategy like this allows methods you aren’t interested in to fall through the loop unprocessed. You could also perform some rough-and-ready validation on the incoming document by handling unexpected elements inside an else clause in your code.

Tip #4: Changing date formats
When writing date formats out to an XML stream, you’ll probably run into trouble getting .NET’s DateTime data type into the correct XML date format so that it can be validated by an XML schema. Try using the XmlConvert class, which has a ToString method overload specifically designed to let you coerce DateTime values into an appropriate string format.

Tip #5: Validating DOM documents
The .NET reader that supports DOM parsing, XmlNodeReader, doesn’t do validation, and it can’t be created from an instance of another XmlReader class. So basing it on XmlValidatingReader is out. To validate an XML document when using the DOM, try something similar to the following:
XmlDocument doc = new XmlDocument();
XmlTextReader trdr = new XmlTextReader(“c:\\somefile.xml”);
XmlValidatingReader reader = new XmlValidatingReader(trdr);
XmlNodeReader rdr = new XmlNodeReader(doc);

Now it’s your turn

Share some of your .NET XML tips by posting them to the discussion below. Or if you’re the shy type, you can e-mail them to the editors. If we get enough tips, we’ll compile them into a downloadable reference guide.