You can create a XML document, by using the basic XmlTextWriter class. This class works like StreamWriter relative, except that it writes XML document instead of ordinary text file. You can follow the next process:
1. Create or open the file
2. Write to file, moving from top to bottom.
3. Close the file.
The next code lines present this idea.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
namespace WriteToXMLFileVC
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
WriteToXMLFile();
}
}
protected void WriteToXMLFile(){
// Place the file in the App_Data subfolder of the current website.
// The System.IO.Path class makes it easy to build the full file name.
string file = Path.Combine(Request.PhysicalApplicationPath,@”App_Data\BooksList.xml”);
FileStream fs = new FileStream(file, FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs, null);w.WriteStartDocument();
w.WriteStartElement(“BooksList”);
w.WriteComment(“This file generated by the XmlTextWriter class.”);// Write the first book.
w.WriteStartElement(“Book”);
w.WriteAttributeString(“ISBN-13”, “978-0545139700”);
w.WriteAttributeString(“Title”, “Harry Potter and the Deathly Hallows”);w.WriteStartElement(“Author”);
w.WriteString(“J.K. Rowling”);
w.WriteEndElement();w.WriteStartElement(“Price”);
w.WriteString(“14.99”);
w.WriteEndElement();w.WriteStartElement(“Available”);
w.WriteString(“True”);
w.WriteEndElement();w.WriteEndElement();
// Write the second book.
w.WriteStartElement(“Book”);
w.WriteAttributeString(“ISBN-13”, “978-1451648539”);
w.WriteAttributeString(“Title”, “Steve Jobs”);w.WriteStartElement(“Author”);
w.WriteString(“Walter Isaacson”);
w.WriteEndElement();w.WriteStartElement(“Price”);
w.WriteString(“14.88”);
w.WriteEndElement();w.WriteStartElement(“Available”);
w.WriteString(“True”);
w.WriteEndElement();w.WriteEndElement();
// Write the third book
w.WriteStartElement(“Book”);
w.WriteAttributeString(“ISBN-13”, “978-0765309761”);
w.WriteAttributeString(“Title”, “Tunnel Vision”);w.WriteStartElement(“Author”);
w.WriteString(“Gary Braver”);
w.WriteEndElement();w.WriteStartElement(“Price”);
w.WriteString(“14.15”);
w.WriteEndElement();w.WriteStartElement(“Available”);
w.WriteString(“False”);
w.WriteEndElement();w.WriteEndElement();
// Close the root element.
w.WriteEndElement();
w.WriteEndDocument();
w.Close();}
}
}
Notes:
– You create the entire XML document by calling the methods of the XmlTextWriter, in the right order. To start a document, you always begin by calling WriteStartDocument(). To end it, you call WriteEndDocument().
– The next step is writing the elements you need. You write elements in three steps. First, you write the start tag (like <Book>) by calling WriteStartElement(). Then you write attributes, elements, and text content inside. Finally, you write the end tag (like </Book>) by calling WriteEndElement().
– The methods you use always work with the current element. So if you call WriteStartElement() and follow it up with a call to WriteAttributeString(), you are adding an attribute to that element. Similarly, if you use WriteString(), you insert text content inside the current element, and if you use WriteStartElement() again, you write another element, nested inside the current element.
If you compile and execute the code the result is a file BooksList.xml. To check that your code worked, open the file in Internet Explorer, which automatically provides a collapsible view for XML documents: