Creating XML file in C# made easy

Before we start just a brief introduction about XML files and how it differs from HTML.
XML is a format for storing and transporting data whereas HTML is used to display data.
I.e. XML is designed to be read by machines and HTML by human beings.
HTML has fixed set of tags. But XML enables users to define their own tags. Hence XML is also a standard for creating markup languages. This is done with the help of DTDs (Data Type Definition) or Schemas. DTDs define the structure of data in the XML file. All permissible tags and attribute are defined in the DTD file and the actual XML file has to be validated against the DTD.

We are creating a simple XML file with the basic nodes.

The file will have

1. XML Declaration
2. XML Comments
3. XML Document Type
4. Root Node
5. Child node with Attribute
6. Child node with Inner Childs

Note: System.XML namespace provides all the types for creating and manipulating XML.


XmlDocument xmlDoc;
XmlNode xmlNode;
XmlElement xmlRootElement;
XmlElement xmlChildElement;
XmlElement xmlInnerChildElement;
XmlText xmlText;
XmlDeclaration xmlDeclatation;
XmlDocumentType xmlDocumentType;

string outputFileName;


 xmlDoc = new XmlDocument();

 //Creating the XML Declaration
 xmlDeclatation = xmlDoc.CreateXmlDeclaration("1.0", "", "yes");

 //Adding Comment Line after Declration
 xmlNode = xmlDoc.CreateNode(XmlNodeType.Comment, "DDD", "DDD");
 xmlNode.Value = "Sample XML File";

 //Adding Doctype
 xmlDocumentType = xmlDoc.CreateDocumentType("DocType", null, null, null);
 //Adding the Root Node IDEA_IMPORT_FIELDS
 xmlRootElement = xmlDoc.CreateElement("", "CONCEPTS", "");

 //Adding Child with Attribute
 xmlChildElement = xmlDoc.CreateElement("", "Concept1", "");
 xmlText = xmlDoc.CreateTextNode("Introduction to XML");
 xmlChildElement.SetAttribute("Subject", "XML");

 //Adding Child2
 xmlChildElement = xmlDoc.CreateElement("", "Concept2", "");
 xmlText = xmlDoc.CreateTextNode("");

 //Adding Inner Child
 xmlInnerChildElement = xmlDoc.CreateElement("", "Concept3", "");
 xmlText = xmlDoc.CreateTextNode("This is an Inner Child");

 //Saving the XML file
 outputFileName = @"C:\newxml.xml";
catch (System.Exception ex)
   //MessageBox.Show("Unable to create XML file");

    xmlDoc = null;
    xmlNode = null;
    xmlRootElement = null;
    xmlChildElement = null;
    xmlChildElement = null;
    xmlText = null;
    xmlDeclatation = null;


<?xml version="1.0" standalone="yes"?>

<!–Sample XML File –>

<!DOCTYPE DocType>


     <Concept1 Subject="XML">Introduction to XML</Concept1>


              <Concept3>This is an Inner Child</Concept3>



1 Comment

  1. Yanesh Tyagi said,

    May 31, 2010 at 2:06 pm

    Nice code. It will be helpful if you explain the concepts and code in more details.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: