free web hosting | free website | Web Hosting | Free Website Submission | shopping cart | Promoter Online | php hosting
affordable web hosting Pets web page hosting web hosting website hosting web hosting service web hosting web host
XmlTextWriter: Writing XML data to a File

XmlTextWriter: Writing XML data to a File

This page explains how to write XML data to a file in Visual C++ .NET, with XmlTextWrite class.

Shown below is the flow of the procedure to write XML data to a file. First, construct the XmlTextWriter class, specify the formatting attributes, write the 'header' of the XML file, write the elements, and then close the writer.


        XmlTextWriter* pWriter;

	try
	{
		pWriter = new XmlTextWriter (fileName, NULL);

		pWriter->Formatting = Formatting::Indented;
		pWriter->WriteStartDocument(true);

		//write the elements to the file

		pWriter->Flush();
		pWriter->Close();
	}
        catch (Exception* e)
	{
		Console::WriteLine (S"Exception: {0}", e->ToString());
	}
	__finally
	{
		if (pWriter != 0)
			pWriter->Close();
	}

Start writing the node with WriteStartElement. The sub-tree and sub-elements should come right after this. Then call WriteEndElement to close the node.


        pWriter->WriteStartElement("labels");
	...
        pWriter->WriteEndElement();

    

If the node is a leaf that has no sub-tree nor sub-elements call WriteElementString. Specify the name of the element and its value as its arguments.


	pWriter->WriteElementString(S"image", S"Gladiolus.jpg");

According to the XML guideline, the text not to be exposed to users should be specified as attributes, such as color or type face. To add an attribute to the node, call WriteAttributeString and set the name of the attribute and its value as its arguments.


        pWriter->WriteAttributeString(S"color", S"blue");

For more information:
Return to Home Page
Erica Asai
Last Modified: Fri Dec 02 02:49:52 2005