Thursday, 19 July 2012

.Net Tip , C# Tip : Create XML File using XmlWriter and save with C# Examples and VB.Net Examples

You can create a XML File using XMLWriter class.
XMLWriter class available in System.XML namespace.
This will give proper indentation and create Well formed XML File.

Here is example for this.
In this example we create BookList.xml file with different Elements and Attributes.

C# Example :
        XmlWriterSettings objSetting = new XmlWriterSettings();
        objSetting.Indent = true;
        objSetting.NewLineOnAttributes = true;
        
        System.Text.StringBuilder sb = new System.Text.StringBuilder();


        using (XmlWriter objWriter = XmlWriter.Create(sb, objSetting))
        {
            //Note the artificial, but useful, indenting
            objWriter.WriteStartDocument();

            objWriter.WriteStartElement("books");

            ////////Start Book Element///////
            
            objWriter.WriteStartElement("book");

            objWriter.WriteStartAttribute("ISBN");
            objWriter.WriteValue("asp1");
            objWriter.WriteEndAttribute();

            objWriter.WriteStartElement("Title");
            objWriter.WriteValue("ASP.NET");
            objWriter.WriteEndElement();

            objWriter.WriteElementString("ReleaseDate", "11/11/2010");

            objWriter.WriteStartElement("Pages");
            objWriter.WriteValue(200);
            objWriter.WriteEndElement(); //price

            objWriter.WriteEndElement(); //book
            ////////End Book Element///////


            ////////Another Element

            ////////Start Book Element///////
            
            objWriter.WriteStartElement("book");

            objWriter.WriteStartAttribute("ISBN");
            objWriter.WriteValue("c#2");
            objWriter.WriteEndAttribute();

            objWriter.WriteStartElement("Title");
            objWriter.WriteValue("C#.NET");
            objWriter.WriteEndElement();

            objWriter.WriteElementString("ReleaseDate", "10/11/2010");

            objWriter.WriteStartElement("Pages");
            objWriter.WriteValue(500);
            objWriter.WriteEndElement(); 

            objWriter.WriteEndElement(); //book
            ////////End Book Element///////


            
            objWriter.WriteEndElement(); //books
            objWriter.WriteEndDocument();
            
        }

        File.WriteAllText(Server.MapPath("BooksList.xml"), sb.ToString());

VB.net Example :
        Dim objSetting As New XmlWriterSettings()
        objSetting.Indent = True
        objSetting.NewLineOnAttributes = True

        Dim sb As New System.Text.StringBuilder()


        Using objWriter As XmlWriter = XmlWriter.Create(sb, objSetting)
            'Note the artificial, but useful, indenting
            objWriter.WriteStartDocument()

            objWriter.WriteStartElement("books")

            ''''Start Book Element

            objWriter.WriteStartElement("book")

            objWriter.WriteStartAttribute("ISBN")
            objWriter.WriteValue("asp1")
            objWriter.WriteEndAttribute()

            objWriter.WriteStartElement("Title")
            objWriter.WriteValue("ASP.NET")
            objWriter.WriteEndElement()

            objWriter.WriteElementString("ReleaseDate", "11/11/2010")

            objWriter.WriteStartElement("Pages")
            objWriter.WriteValue(200)
            objWriter.WriteEndElement()
            objWriter.WriteEndElement() 'book
            ''''End Book Element

            ''''Another Element

            ''''Start Book Element

            objWriter.WriteStartElement("book")

            objWriter.WriteStartAttribute("ISBN")
            objWriter.WriteValue("c#2")
            objWriter.WriteEndAttribute()

            objWriter.WriteStartElement("Title")
            objWriter.WriteValue("C#.NET")
            objWriter.WriteEndElement()

            objWriter.WriteElementString("ReleaseDate", "10/11/2010")

            objWriter.WriteStartElement("Pages")
            objWriter.WriteValue(500)
            objWriter.WriteEndElement()
            objWriter.WriteEndElement() 'book
            ''''End Book Element


            objWriter.WriteEndElement() 'books

            objWriter.WriteEndDocument()
        End Using

        File.WriteAllText(Server.MapPath("BooksList.xml"), sb.ToString())

Output (Generated XML) :

<?xml version="1.0" encoding="utf-16"?>
<books>
  <book
    ISBN="asp1">
    <Title>ASP.NET</Title>
    <ReleaseDate>11/11/2010</ReleaseDate>
    <Pages>200</Pages>
  </book>
  <book
    ISBN="c#2">
    <Title>C#.NET</Title>
    <ReleaseDate>10/11/2010</ReleaseDate>
    <Pages>500</Pages>
  </book>
</books>

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that. 



1 comment:

  1. Thanks so much. This really helpse!

    I have a situation where the attribute of an element contain the character ":" (

    When I use the vb.net code as follows:

    writer.WriteStartAttribute("xmlns:xsi")

    VS2010 throws an error indicating that the character ":" is not allowed. Can you please advise as to how to get around this?

    Thanks so much!

    Justin

    ReplyDelete