Sunday, 27 January 2013

Writing xml file using XMLDocument VB.NET

Writing xml file using XMLDocument VB.NET

   Creating  an XML file using XMLDocument 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 Create author node
                              Step Create firstname element  add it to author node
                               Step 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 CreateXMLFile()
            Dim doc As XmlDocument =  New XmlDocument()
            XmlProcessingInstruction xmlPI=
            doc.CreateProcessingInstruction("xml", "version='1.0'")

       Dim root As XmlElement = doc.CreateElement("books")
            Dim books As XmlNode =  doc.AppendChild(root)
            Dim book As XmlElement = doc.CreateElement("book")
            'XmlAttribute idattribute =
            book.SetAttribute("id", "1")'doc.CreateAttribute("id");
            'idattribute.Value = "1";
            Dim title As XmlElement =  doc.CreateElement("title")  title.InnerText  =  "C# Programming"
            Dim Price As XmlElement =  doc.CreateElement("Price")  Price.InnerText  =  "$44.49"
            Dim Publisher As XmlElement =  doc.CreateElement("Publisher")  Publisher.InnerText  =  "APRESS"

            Dim authors As XmlElement = doc.CreateElement("Authors")
            Dim author As XmlElement = doc.CreateElement("author")
            Dim firstname As XmlElement =  doc.CreateElement("firstname")  firstname.InnerText  =  "jhon"
            Dim lastname As XmlElement =  doc.CreateElement("lastname")  lastname.InnerText  =  "peter"
            Dim author.AppendChildCType(As author.AppendChild(firstname), lastname)



  End Sub

<?xml version='1.0'?>
  <book id="1">
    <title>C# Programming</title>

Tags: Writing xml file using XMLDocument VB.NET,Writing an xml file using XMLDocument VB.NET,Creating an XML file using VB.NET, Create XML file using XMLDocument, Create an XML file using XMLDocument,System.Xml.XMLDocument,, Creating an XML in ASP.NET,Creating an XML in WPF. Creating an XML in Windows Forms.