How to create an XML file

22 01 2012

In one of the previous posts we have seen how to read an XML file, similar way sometimes we may need to create an XML file from our applications.

To read and write and XML file Microsoft provides an XML parser know as MSXML. This is a COM component that comes with different MS Products like MS Windows, MS IEs, MS Office etc.

XMLDomWriter.h

XMLDomWriter.cpp

 

 

#include "XMLDomWriter.h"

void CreateXMLFile()
{
	CXMLNode		rootNode;
	CXMLDomDocument doc;

	rootNode = doc.CreateRootNode( _T("RootNode"));
	rootNode.AddNewLine();
	CXMLNode firstNode(doc.CreateNode(_T("Node1")));
	rootNode.AppendNode( firstNode );
	firstNode.AddNewLine();
	firstNode.SetAttributes( _T("AttributeName"), _T("AttributeValue"));
	CXMLNode childNode(doc.CreateNode(_T("ChildNode")));
	childNode.SetValue(_T("NodeValue"));
	firstNode.AddChildNode( childNode );
	firstNode.AddNewLine();
	rootNode.AddNewLine();

	doc.Save("c:\\test.xml");
}

Please download the attached XMLDomWriter.h and XMLDOMWriter.cpp files and include it into your project. You may need to change the msxml dll import section of header file as per the compiler version.

Advertisements