- Create XML File
Private Sub CreateXmlFile()Dim xw As New XmlTextWriter("C:\mySampleXml.xml", System.Text.Encoding.UTF8)xw.WriteStartDocument()xw.WriteStartElement("nodes", "")xw.WriteStartElement("node", "")xw.WriteStartAttribute("id", "")xw.WriteString("1")xw.WriteEndAttribute()xw.WriteStartElement("child")xw.WriteString("sample10")xw.WriteEndElement()xw.WriteStartElement("child")xw.WriteString("sample11")xw.WriteEndElement()xw.WriteEndElement()xw.WriteStartElement("node", "")xw.WriteStartAttribute("id", "")xw.WriteString("2")xw.WriteEndAttribute()xw.WriteStartElement("child")xw.WriteString("sample20")xw.WriteEndElement()xw.WriteStartElement("child")xw.WriteString("sample21")xw.WriteEndElement()xw.WriteEndElement()xw.WriteStartElement("node", "")xw.WriteStartAttribute("id", "")xw.WriteString("3")xw.WriteEndAttribute()xw.WriteStartElement("child")xw.WriteString("sample30")xw.WriteEndElement()xw.WriteStartElement("child")xw.WriteString("sample31")xw.WriteEndElement()xw.WriteEndElement()xw.WriteEndElement()xw.Flush()xw.Close()Process.Start("C:\mySampleXml.xml")End Sub- Insert Data in XML File
Private Sub InsertDataToXmlFile()Dim xDoc As New XmlDocumentxDoc.Load("C:\mySampleXml.xml")Dim sourceRootNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]") 'Slects the root 'nodes'Dim newChildNode As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "child", "")newChildNode.InnerText = "New Sample 22"'Child with attributeDim newChildNode1 As XmlNode = xDoc.CreateNode(XmlNodeType.Element, "child", "")Dim atr As XmlAttribute = xDoc.CreateAttribute("childID")atr.Value = "ch1"newChildNode1.InnerText = "New Sample 23"newChildNode1.Attributes.Append(atr)sourceRootNode.AppendChild(newChildNode)sourceRootNode.AppendChild(newChildNode1)xDoc.Save("C:\mySampleXml.xml")Process.Start("C:\mySampleXml.xml")End Sub- Modify Data in XML File
Private Sub ModifyDataInXmlFile()Dim xDoc As New XmlDocumentxDoc.Load("C:\mySampleXml.xml")Dim desiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")Dim childNodes As XmlNodeList = desiredNode.SelectNodes("./child") ''.' specifies the current node and '/child' specifies child nodesFor Each curNode As XmlNode In childNodesIf curNode.HasChildNodes = True ThenIf curNode.Attributes.Count > 0 ThenIf curNode.Attributes("childID").Value = "ch1" ThencurNode.InnerText = "Updated Sample 23"End IfEnd IfEnd IfNextxDoc.Save("C:\mySampleXml.xml")Process.Start("C:\mySampleXml.xml")End Sub- Delete Data in XML File
Private Sub RemoveDataFromXmlFile()Dim xDoc As New XmlDocumentxDoc.Load("C:\mySampleXml.xml")Dim desiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")Dim childNodes As XmlNodeList = desiredNode.SelectNodes("./child") ''.' specifies the current node and '/child' specifies child nodesFor Each curNode As XmlNode In childNodesIf curNode.HasChildNodes = True ThenIf curNode.Attributes.Count > 0 ThenIf curNode.Attributes("childID").Value = "ch1" ThencurNode.ParentNode.RemoveChild(curNode)End IfEnd IfEnd IfNextDim anotherDesiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")anotherDesiredNode.ParentNode.RemoveChild(anotherDesiredNode)xDoc.Save("C:\mySampleXml.xml")Process.Start("C:\mySampleXml.xml")End SubPlease give any suggestions for better coding. Also add code for other possible cases that can be possible for other various situations.- Create XML File
Tuesday, April 14, 2009
XML Operations: Create XML, Insert Data, Modify Data and Delete Data in XML File
Subscribe to:
Post Comments (Atom)
1 comment:
In some cases we need to create empty xml document. Like if file not exists then create and also if we slipped the way from mind then here is the tip:
If IO.File.Exsists(filename)=False Then
Dim xw As New XmlTextWriter(filename,System.Text.Encoding.UTF8)
xw.Flush()
xw.Close
End If
Post a Comment