Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

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 ]

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 

1 | 2 | 3 | 4


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com