Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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:




This post first appeared on Practical C# Codes, please read the originial post: here

Share the post

Xml Document creation using DOM

×

Subscribe to Practical C# Codes

Get updates delivered right to your inbox!

Thank you for your subscription

×