Sunday, 27 January 2013

Writing xml file using XmlWriter VB.NET

Writing xml file using XmlWriter VB.NET

   Creating  an XML file using XmlWriter part of System.Xml namespace.
Step 1) Create Processing Instruction
Step 2) Create root element called "books" 
Step 3) Create book element
         Step 3.1) Create book title and add it to book
         Step 3.2) Create book price and add it to book
         Step 3.3) Create book publisher and add it to book
                  Step 3.3.1) Create a Authors Node
                  Step 3.3.1.1) Create author node
                              Step 3.3.1.1.1) Create firstname element  add it to author node
                               Step 3.3.1.1.1) Create lastname element       add it to author node    
                   Step 3.3.2) add author node to Authors Node
                  Step   3.3.3)add authors node to book node
Step 4) Add book node to books node
Step 5) Save the XML document                 

    Shared  Sub CreateXMLusingXMlWriter()
           Try
               Imports (XmlWriter writer = XmlWriter.Create("c:\books2.xml"))
               {

                   ' writer.WriteProcessingInstruction("xml", "version='1.0'");
                   writer.WriteStartDocument(True)
                   writer.WriteStartElement("books")
                   writer.WriteStartElement("book")

                   writer.WriteStartElement("title")
                   writer.WriteString("C# Programming")
                   writer.WriteEndElement()

                   writer.WriteStartElement("price")
                   writer.WriteString("Rs.67888")
                   writer.WriteEndElement()

                   writer.WriteStartElement("publisher")
                   writer.WriteString("APRESS")
                   writer.WriteEndElement()

                   writer.WriteStartElement("Authors")
                       writer.WriteStartElement("author")
                       writer.WriteStartElement("firstname") writer.WriteString("jhon") writer.WriteEndElement()
                       writer.WriteStartElement("lastname") writer.WriteString("peter") writer.WriteEndElement()
                       writer.WriteEndElement()
                       writer.WriteEndElement()'authors

                   writer.WriteEndElement()'book

                   writer.WriteEndElement()
                   writer.WriteEndDocument()

               }
           Catch ex As Exception
               Console.WriteLine(ex.Message)
           End Try
   End Sub


OUTPUT<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<books>
<book>
      <title>C# Programming</title>
     <price>Rs.67888</price>
     <publisher>APRESS</publisher>
     <Authors>
           <author>
                     <firstname>jhon</firstname>
                     <lastname>peter</lastname>
            </author>
     </Authors>
</book>
</books>
Tags: Writing xml file using XmlWriter VB.NET,Writing an xml file using XmlWriter VB.NET,Creating an XML file using VB.NET, Create XML file using XmlWriter,Create an XML file using XmlWriter, System.Xml.XmlWriter, XMLDocumet.save, Creating an XML in ASP.NET,Creating an XML in WPF. Creating an XML in Windows Forms.