dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: .NET
Expertise: Beginner
Apr 3, 2009

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 version="1.0" encoding="utf-8"?>
<CategoryList>
  <Category ID="01">
    <MainCategory>XML</MainCategory>
    <Description>This is a list of my XML articles.</Description>
    <Active>true</Active>
  </Category>
</CategoryList>

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("<CategoryList>");

   // declare child node.
   Document.AppendLine("<Category ID=\"01\">"); //We could tab indention

   // add child node's child node
   Document.AppendLine("<MainCategory>XML</MainCategory>");

   // add another child node
   Document.AppendLine("<Description>This is a list of my XML articles.</Description>");

   // and another
   Document.AppendLine("<Active>true</Active>");

   // close the Category node.
   Document.AppendLine("</Category>");

   // close the root node
   Document.AppendLine("</CategoryList>");

   // 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("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
   return (BaseDocument);
}

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

Julio Henriquez
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date