Home > Articles > Web Services > XML

  • Print
  • + Share This
Like this article? We recommend

Generating an XML File

Before you can do much with XML, you need to understand how to format an XML file and how to generate it. I'll assume that you already know how to format an XML file.

NOTE

If you need a refresher on the topic, check out this XML primer. You should also look through this ZVON.org tutorial.

After you understand XML formatting, it's time to create an XML document using Visual C++ .NET. Listing 2 shows a simple method to generate XML.

Listing 2 Generating an XML File using .NET

System::Void btnGenerate_Click(System::Object * sender, 
                System::EventArgs * e)
{
  XmlTextWriter* DataWrite; // Performs the actual data write.

  // Create the data writer.
  DataWrite = new XmlTextWriter(txtFilename->Text, 
                 System::Text::Encoding::UTF8);

  // Write the XML header. When you set this function to true,
  // .NET creates a standalone document.
  DataWrite->WriteStartDocument(true);
  DataWrite->WriteWhitespace("\r\n");

  // Describe the test file.
  DataWrite->WriteComment("This is a test document.");
  DataWrite->WriteWhitespace("\r\n");

  // Start the document. You must specify true to create a 
  // new document with a root element. Failure to include a
  // root element results in an error. This example includes
  // a namespace prefix, the local name of the element, and 
  // an URL to associate with the namespace.
  DataWrite->WriteStartElement("Data", 
                "MyData", 
                "http://www.mysite.com/");
  DataWrite->WriteWhitespace("\r\n");

  // Write the data to the document.
  DataWrite->WriteElementString("DataString1", txtData->Text);
  DataWrite->WriteWhitespace("\r\n");

  // Write the same data with a special attribute.
  DataWrite->WriteStartElement("DataString2");
  DataWrite->WriteAttributeString("AnAttribute", "AttributeValue");
  DataWrite->WriteString(txtData->Text);
  DataWrite->WriteEndElement();
  DataWrite->WriteWhitespace("\r\n");

  // End the starting element or the root element of the document.
  DataWrite->WriteEndElement();

  // End the document.
  DataWrite->WriteEndDocument();

  // Close the document. This call includes functionality to flush
  // the content of the buffer to disk.
  DataWrite->Close();

  // Indicate the document is complete.
  MessageBox::Show("File Generated",
          "XML Generation Results",
          MessageBoxButtons::OK,
          MessageBoxIcon::Information);
}

Every XML writing task begins when you create an XmlTextWriter object—DataWrite in this case. You use special methods to write the XML document output. The .NET Framework requires that you use some methods to produce well-formed XML. For example, you must use the WriteStartDocument() method to define the beginning of the document, which is the XML header. The writing session ends when you use the WriteEndDocument() method to close this tag and Close() to flush the contents of the document buffer to disk and perform the actual file closure.

XML documents require a root element. The first WriteStartElement() call and associated WriteEndElement() call create this root element. The example places two kinds of child elements within this root element. The first shows how to write a string value without any attributes using the WriteElementString() method. The second shows how to add an attribute to the element using the WriteAttributeString() method.

The example also includes a few optional elements. Some developers don't care how the output file appears, but I often need to view the output in a text editor. Using the WriteWhitespace() method to add carriage returns and tabs (if desired) makes the resulting file a lot easier to read in a text editor. Of course, applications such as Internet Explorer and the Visual Studio IDE don't care about the white space.

You might also want to include comments to document the file. Use the WriteComment() method to accomplish this task. Figure 1 shows the output of the generation cycle when viewed in Internet Explorer.

Figure 1Figure 1 The sample application generates typical XML data.

  • + Share This
  • 🔖 Save To Your Account