MINI SUPPORTERS:

 

 

Writing/Saving XML Files - Page 4
       by kirupa  |  20 June 2007

In the previous page you finished learning how to save XML data containing elements, nested elements, and attributes to a file. In this page, I will provide a more modular solution that makes it easy to add new, in our case, books without having to write more lines of code for each book.

The following is my version:

private XmlDocument xmlDoc = new XmlDocument();
private XmlElement booksElement;
 
private void AddBook(string ISBN, string title, string author)
{
//
// Initialize booksElement if you are adding your first book.
//
if (booksElement == null)
{
booksElement = xmlDoc.CreateElement("Books");
}
 
//
// Same as tutorial; Adding and populating the elements
//
XmlElement bookElement = xmlDoc.CreateElement("Book");
 
XmlAttribute bookAttribute = xmlDoc.CreateAttribute("ISBN");
bookElement.SetAttributeNode(bookAttribute);
bookAttribute.Value = ISBN;
 
XmlElement titleElement = xmlDoc.CreateElement("title");
titleElement.InnerText = title;
bookElement.AppendChild(titleElement);
 
XmlElement authorElement = xmlDoc.CreateElement("author");
authorElement.InnerText = author;
bookElement.AppendChild(authorElement);
 
booksElement.AppendChild(bookElement);
}
 
private void WriteToDisk(string path)
{
// Add the booksElement to our root element and write to disk
xmlDoc.AppendChild(booksElement);
xmlDoc.Save(path);
}
 
 
static void Main(string[] args)
{
 
Program bookList = new Program();
 
bookList.AddBook("0553212419", "Sherlock Holmes: Complete Novels and Stories, Vol 1", "Sir Arthur Conan Doyle");
bookList.AddBook("0743273567", "The Great Gatsby", "F. Scott Fitzgerald");
bookList.AddBook("0684826976", "Undaunted Courage", "Stephen E. Ambrose");
bookList.AddBook("0743203178", "Nothing Like It In the World", "Stephen E. Ambrose");
 
bookList.WriteToDisk(@"C:\Users\Kirupa\Desktop\books.xml");
}

I am not going to go through my example in great detail, for it is really just a minor extension of what you had been doing earlier. Instead of having all of our application coded in our Main method, I broke up common pieces and placed them in their own methods.

This division, or modularity, allows me to add books using the AddBook method and specifying the ISBN, title, and author information. The AddBook method takes care of populating the bookElement object, and best of all, I can easily add as many books as I want by simply making another call to AddBook.

Once you have finished adding the last book, I make a call to the WriteToDisk method with your argument being the path you wish to save your file to. With that, you are done with this tutorial, and this is how your final XML file should look like:

[ your final XML with all of the information we planned to add ]

Need Help?

If you have questions, need some assistance on this topic, or just want to chat - please drop by our friendly forums and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out. Plus, we have a large collection of smileys you can use

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

If you didn't like it, I always like to hear how I can do better next time. Please feel free to contact me directly at kirupa[at]kirupa.com.

Cheers!

Kirupa Chinnathambi
about | facebook | twitter

 

1 | 2 | 3 | 4

SUPPORTERS:

cloud storage
cloud storage
kirupa.com's fast and reliable hosting provided by Media Temple. Creative web apps. Make your own free flash banners and photo slideshows.
HTML5 CSS3 Mobile Gallery for iPhone, iPad Flash effects. Art without coding.
Flipping Book - page flip flash component. Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery
X-Platform Application Development for Flash Free Flash Components Download - XML Templates, Players and Galleries.

two computer monitors

US Direct

Learn how to advertise on kirupa.com  
 
SHARE:



MINI SUPPORTERS: