How to Generate an XML Document Rogrammatically Using DOM: Part I

In fact, I wrote this article as an answer of one among questions posted as a part of a C sharp corner forum. I will enumerate methods of how to do that through three articles, the first one illustrates how to do that using DOM technology, the second step, I'll do the same think using SAX technology and finally I will use some classes provided by the .Net framework to achieve the same task.

First way as said, using DOM technology witch is the "Document Object Model"   abbreviation witch is largely  adopted by several platforms across the development environments. It consists on an in memory XML document representation that not only allows xml generation, but allows several tasks such  as manipulation, reading and modification of a given xml document. I illustrate how to use DOM by this walkthrough called Walkthrough1:

Walkthrough 1:

Create a new console application project then add this code within the scope of the main method:

//Create a new xmlDataDocument

XmlDataDocument myDoc = new XmlDataDocument();

//The familie

XmlElement Familie = myDoc.CreateElement("Familie");

//The elements wrapper

XmlElement wrapper = myDoc.CreateElement("xml");

//Create attributes

XmlAttribute version = myDoc.CreateAttribute("version");

XmlAttribute encoding = myDoc.CreateAttribute("encoding");

//Add values to attributes

version.Value = "1.0";

encoding.Value = "utf-8";

//Append attributes to their correspondent element

wrapper.Attributes.Append(version);

wrapper.Attributes.Append(encoding);

//The Father

XmlElement Father = myDoc.CreateElement("Father");

//Create attributes

XmlAttribute FatherName = myDoc.CreateAttribute("Name");

XmlAttribute FatherAge = myDoc.CreateAttribute("Age");

//Add values to attributes

FatherName.Value = "My father";

FatherAge.Value = "65";

//Append attributes to their correspondent element

Father.Attributes.Append(FatherName);

Father.Attributes.Append(FatherAge);

//Add the element to the Familie node

Familie.AppendChild(Father);

//The mother

XmlElement Mother = myDoc.CreateElement("Mother");

//Create attributes

XmlAttribute MotherName = myDoc.CreateAttribute("Name");

XmlAttribute MotherAge = myDoc.CreateAttribute("Age");

//Add values to attributes

MotherName.Value = "My mother";

MotherAge.Value = "60";

//Append attributes to their correspondent element

Mother.Attributes.Append(MotherName);

Mother.Attributes.Append(MotherAge);

//Add the element to the Familie node

Familie.AppendChild(Mother);

//The sister

XmlElement Sister = myDoc.CreateElement("Sister");

//Create attributes

XmlAttribute SisterName = myDoc.CreateAttribute("Name");

XmlAttribute SisterAge = myDoc.CreateAttribute("Age");

//Add values to attributes

SisterName.Value = "My sister";

SisterAge.Value = "20";

//Append attributes to their correspondent element

Sister.Attributes.Append(SisterName);

Sister.Attributes.Append(SisterAge);

//Add the element to the Familie node

Familie.AppendChild(Sister);

//The brother

XmlElement Brother = myDoc.CreateElement("Brother");

//Create attributes

XmlAttribute BrotherName = myDoc.CreateAttribute("Name");

XmlAttribute BrotherAge = myDoc.CreateAttribute("Age");

//Add values to attributes

BrotherName.Value = "My brother";

BrotherAge.Value = "21";

//Append attributes to their correspondent element

Brother.Attributes.Append(BrotherName);

Brother.Attributes.Append(BrotherAge);

//Add the element to the Familie node

Familie.AppendChild(Brother);

//wrapp all elements nodes in wrapper node 

wrapper.AppendChild(Familie);

//Add the entire node in to the docuement

myDoc.AppendChild(wrapper);

//Save the xml document

myDoc.Save(@"C:\myFamilie.xml");

//Give a signal that all gonna be all  right

Console.WriteLine("Document is generated successfully!!");
Console.Read();

Now, run the application and wait until the message "Document is generated successfully!!" is displayed then browse to   C:\myFamilie.xml and observe.

Good dotneting!!!


Similar Articles