Tuesday, April 14, 2009

XML Operations: Create XML, Insert Data, Modify Data and Delete Data in XML File

    • 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 XmlDocument
            xDoc.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 attribute
            Dim 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 XmlDocument
            xDoc.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 nodes
            For Each curNode As XmlNode In childNodes
                If curNode.HasChildNodes = True Then
                    If curNode.Attributes.Count > 0 Then
                        If curNode.Attributes("childID").Value = "ch1" Then
                            curNode.InnerText = "Updated Sample 23"
                        End If
                    End If
                End If
            Next
            xDoc.Save("C:\mySampleXml.xml")
            Process.Start("C:\mySampleXml.xml")

        End Sub


    • Delete Data in XML File
        Private Sub RemoveDataFromXmlFile()
            Dim xDoc As New XmlDocument
            xDoc.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 nodes
            For Each curNode As XmlNode In childNodes
                If curNode.HasChildNodes = True Then
                    If curNode.Attributes.Count > 0 Then
                        If curNode.Attributes("childID").Value = "ch1" Then
                            curNode.ParentNode.RemoveChild(curNode)
                        End If
                    End If
                End If
            Next
            Dim anotherDesiredNode As XmlNode = xDoc.SelectSingleNode("/nodes/node[@id=2]")
            anotherDesiredNode.ParentNode.RemoveChild(anotherDesiredNode)
            xDoc.Save("C:\mySampleXml.xml")
            Process.Start("C:\mySampleXml.xml")

        End Sub

    Please give any suggestions for better coding. Also add code for other possible cases that can be  possible for other various situations.

1 comment:

jivangoyal said...

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