Search:

Custom Search
_________________________________________________________________

Friday, July 16, 2010

Xml Document creation using DOM

In this article I´ll show you how to create a .xml document using proper tags and definitions. To do this you need to import the System.Xml library.

The idea is simple and the best way to learn this is by a small example.


Here I´ll create a XmlDocument with a couple of child elements.


Code:


XmlDocument xmlDoc = new XmlDocument();

//First, create the declaration


XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

xmlDoc.AppendChild(declaration);

XmlElement books = xmlDoc.CreateElement("BOOK");

xmlDoc.AppendChild(books);


//Child 1 for BOOK

XmlElement book1 = xmlDoc.CreateElement("book-1");

book1.SetAttribute("name", "Harry Potter");

books.AppendChild(book1);


//Child 2 for BOOK

XmlElement book2 = xmlDoc.CreateElement("book-2");

book2.SetAttribute("name", "People");

book2.SetAttribute("type", "magazine");

books.AppendChild(book2);


//Save the Xml

xmlDoc.Save(@"C:\XML\" + "xmlTest.xml");


Output:


1 comment:

Litéra said...

I will try this Code in My Project & again i will get back You.


Thanks

Excel Comparison