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 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 CreateXMLFile()
Dim doc As XmlDocument = New XmlDocument()
XmlProcessingInstruction xmlPI=
doc.CreateProcessingInstruction("xml", "version='1.0'")
doc.AppendChild(xmlPI)
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";
'book.AppendChild(idattribute);
Dim title As XmlElement = doc.CreateElement("title") title.InnerText = "C# Programming"
book.AppendChild(title)
Dim Price As XmlElement = doc.CreateElement("Price") Price.InnerText = "$44.49"
book.AppendChild(Price)
Dim Publisher As XmlElement = doc.CreateElement("Publisher") Publisher.InnerText = "APRESS"
book.AppendChild(Publisher)
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)
authors.AppendChild(author)
book.AppendChild(authors)
books.AppendChild(book)
doc.Save("c:\books.xml")
End Sub
OUTPUT
<?xml version='1.0'?>
<books>
<book id="1">
<title>C# Programming</title>
<Price>$44.49</Price>
<Publisher>APRESS</Publisher>
<Authors>
<author>
<firstname>jhon</firstname>
<lastname>peter</lastname>
</author>
</Authors>
</book>
</books>
<books>
<book id="1">
<title>C# Programming</title>
<Price>$44.49</Price>
<Publisher>APRESS</Publisher>
<Authors>
<author>
<firstname>jhon</firstname>
<lastname>peter</lastname>
</author>
</Authors>
</book>
</books>
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, XMLDocumet.save, Creating an XML in ASP.NET,Creating an XML in WPF. Creating an XML in Windows Forms.
No comments:
Post a Comment