web 2.0

Writing XML File Dynamically

Hey Geeks,Writing xml file in .net application is quit general but I have noticed most of the people do this work in a loong and wrong (in my opinion) way. They write the xml file with the help of dataset which is more like a database...... Yes offcourse resource heavy. So let us make this operation quit simple and easy. consider the following XML Schema. We will write the same schema from our code to a local file.
XML Schema :
<?xml version=“1.0“ encoding=“utf-8“?><Items><Item><ItemId>3</ItemId><ProductName>Pine Apple</ProductName><Price>30 $</Price><Item><Items>
VB.net Code :
Dim XMLDoc As New XmlDocumentXMLDoc.Load(Server.MapPath("MyData.xml"))Dim XEle As XmlElement = XMLDoc.CreateElement("Item")Dim XTxt As XmlText'XMLDoc.DocumentElement.AppendChild(XEle)XEle = XMLDoc.CreateElement("ItemID")XTxt = XMLDoc.CreateTextNode("3")XMLDoc.DocumentElement.AppendChild(XEle)XMLDoc.DocumentElement.LastChild.AppendChild(XTxt)XEle = XMLDoc.CreateElement("ProductName")XTxt = XMLDoc.CreateTextNode("Meera")XMLDoc.DocumentElement.AppendChild(XEle)XMLDoc.DocumentElement.LastChild.AppendChild(XTxt)XEle = XMLDoc.CreateElement("Price")XTxt = XMLDoc.CreateTextNode("50 $")XMLDoc.DocumentElement.AppendChild(XEle)XMLDoc.DocumentElement.LastChild.AppendChild(XTxt)XMLDoc.Save(Server.MapPath("MyData.xml"))

Comments

Saving Marriage United States, on 12/17/2009 2:56:24 AM Said:

Saving Marriage

Thanks for the XML tips. It gives me hope. I don't want to be in despair all long as I have the initiative to achieve my dreams. Great blog of yours. Thanks for the wisdom.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading