Creating XML Using the XmlDocument Class

Excerpt by Ken Getz

Most developers have used the XmlDocument class when creating new XML content, and it works fine. The problem is that its use is somewhat opaque. That is, the code isn't readable (it's difficult to discern what the resulting XML will look like as you read it), and it's not always obvious how to create the XML content that you need. Imagine that you need to create just this simple bit of XML content:


You might think creating this content would be simple, if you hadn't worked with the XmlDocument class, but it actually requires a bit of code. The XmlDocument object requires you to take several steps each time you need to add a new node to the root document. Given a reference to the XML document, you must:

  • Call a method of the root node (CreateProcessingInstruction, CreateElement, CreateComment, and so on), creating the new node. You must pass necessary values to the constructor of the new node to initialize the node.
  • Append the new node as a child of an existing node, by calling the AppendChild method of the node.

The sample procedure, CreateXmlDocument, demonstrates creating the XML shown previously, using the pre-LINQ XmlDocument class:

private void CreateXmlDocument() { var custDoc = new XmlDocument(); var declaration = custDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); custDoc.AppendChild(declaration); var comment = custDoc.CreateComment("Sample customer information"); custDoc.AppendChild(comment); var element = custDoc.CreateElement("Customers"); custDoc.AppendChild(element); custDoc.Save(outputFileName); DisplayResults("Contents of " + outputFileName, File.ReadAllText(outputFileName)); }

  The code starts by creating a new XmlDocument object, the root of the XML content:

var custDoc = new XmlDocument();

  As mentioned earlier, for each node you create, you must take two steps, creating the node and then appending it as a child of its new parent. The sample procedure starts by creating and appending the XML declaration:

var declaration = custDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"); custDoc.AppendChild(declaration);

  Then, it creates and appends the comment node:

var comment = custDoc.CreateComment("Sample customer information"); custDoc.AppendChild(comment);

  Finally, the code creates the document element, named Customers, and appends it as a child of the document:

var element = custDoc.CreateElement("Customers"); custDoc.AppendChild(element);

  To show how to create an XML file, the code calls the Save method of the XmlDocument object, and then displays the contents of the file:

 custDoc.Save(outputFileName); DisplayResults("Contents of " + outputFileName, File.ReadAllText(outputFileName));

  NOTE The sample application contains several different overloaded versions of the DisplayResults method, and a few overloads for the DisplayFormattedResults methods. The sample procedures call these "helper" methods, and each displays output in the Console window. The DisplayFormattedResults method calls the String.Format method for you, given a template in the first parameter. If you're interested, you can investigate these methods.

ldn-expertkgetzThis post is an excerpt from the online courseware for our Microsoft LINQ Using Visual C# 2010 course written by expert Ken Getz.

Ken Getz

Ken Getz is a featured instructor for several of our Visual Studio courses. He is a Visual Basic and Visual C# expert and has been recognized multiple times as a Microsoft MVP. Ken is a seasoned instructor, successful consultant, and the author or co-author of several best-selling books. He is a frequent speaker at technical conferences like Tech-Ed, VSLive, and DevConnections and he has written for several of the industry's most-respected publications including Visual Studio Magazine, CoDe Magazine, and MSDN Magazine.

This course excerpt was originally posted October 14, 2013 from the online courseware LINQ Using Visual C# 2010 by Ken Getz