Create an XML File or XmlDocument Directly from a StringBuilder

An earlier DevX tip, “Create a New XML File Using XmlDocument,” showed how to create a new XmlDocument, populate it, and save the results to a file. This tip uses the same XML example, but builds the document with a StringBuilder instead, which is simpler than working directly with the System.Xml.XmlDocument class.

Suppose you want to build this XML:

      XML    This is a list of my XML articles.    true  

The following method shows how to build the preceding document with a StringBuilder. The following code gets a StringBuilder containing the minimal XML, appends strings containing the elements, loads an XmlDocument instance from the StringBuilder contents, and returns the populated XmlDocument object:

public XmlDocument BuildXmlDocument(){   StringBuilder Document = CreateBaseStringBuilder(); // create a generic XML string   //declare the root.   Document.AppendLine("");   // declare child node.   Document.AppendLine(""); //We could tab indention   // add child node's child node   Document.AppendLine("XML");   // add another child node   Document.AppendLine("This is a list of my XML articles.");   // and another   Document.AppendLine("true");   // close the Category node.   Document.AppendLine("");   // close the root node   Document.AppendLine("");   // now read the contents into an XmlDocument   try   {      // Create the new Instance.      XmlDocument XMLDocument = new XmlDocument();      // Add the content      XMLDocument.LoadXml(Document.ToString());      // return it.      return (XMLDocument);   }   catch(Exception ex)   {      // in case of failure return null      return (null);   }}// This function creates a new instance of System.Text.StringBuilder, // adds a Basic XmlDocumentDeclaration, // and returns the new StringBuilder.private StringBuilder CreateBaseStringBuilder(){   StringBuilder BaseDocument = new StringBuilder();   BaseDocument.AppendLine("");   return (BaseDocument);}

Note that you’d need to call the returned XmlDocument’s Save method from the calling function.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS