Friday, February 19, 2010

sample Program for xml File creation,Insert Nodes,Delete Nodes Data and Find data by nodename

Private Function createXML() As String
' This method shows how to build an XML file all from code.
Dim xDoc As New XmlDocument

Dim xPI As XmlProcessingInstruction
Dim xComment As XmlComment
Dim xElmntRoot As XmlElement
Dim xElmntRecalac As XmlElement

xPI = xDoc.CreateProcessingInstruction("xml", "version='1.0'")
xDoc.AppendChild(xPI)

xComment = xDoc.CreateComment("Server Configuration Information")
xDoc.AppendChild(xComment)

xElmntRoot = xDoc.CreateElement("xml")
xDoc.AppendChild(xElmntRoot)

' Rather than creating new nodes individually,
' count on the fact that AppendChild returns a reference
' to the newly added node.
xElmntRecalac = CType(xElmntRoot.AppendChild(xDoc.CreateElement("SERVER_CONFIG")), XmlElement)
xDoc.Save("C:\CEMDAS\DATA\SERVER_CONFIG.xml")
Return xDoc.OuterXml
End Function
Private Sub addElements()
Dim xDoc As New XmlDocument

xDoc.Load("C:\CEMDAS\DATA\SERVER_CONFIG.xml")
Dim xNode As XmlNode
Dim xElmntSERVER_CFG As XmlElement = Nothing
' Search for a particular node
xNode = xDoc.SelectSingleNode("//SERVER_CONFIG")
Dim xnode_SERVER_ID As XmlNodeList
xnode_SERVER_ID = xDoc.SelectNodes("//SERVER_ID")
mintSERVER_ID = xnode_SERVER_ID.Count + 1
If mintSERVER_ID = 0 Then
mintSERVER_ID = 1
End If
If xNode IsNot Nothing Then
If TypeOf xNode Is XmlElement Then
xElmntSERVER_CFG = CType(xNode, XmlElement)
End If

InsertTextNode(xDoc, xElmntSERVER_CFG, "SERVER_ID", mintSERVER_ID)
InsertTextNode(xDoc, xElmntSERVER_CFG, "SERVER_NAME", txtServerName.Text)
InsertTextNode(xDoc, xElmntSERVER_CFG, "SERVER_IP", txtIPADDRESS.Text)
InsertTextNode(xDoc, xElmntSERVER_CFG, "DESCRIPTION", txtSERVER_DESC.Text)
xDoc.Save("C:\CEMDAS\DATA\SERVER_CONFIG.xml")
End If

End Sub
Private Function InsertTextNode(ByVal xDoc As XmlDocument, ByVal xNode As XmlNode, ByVal strTag As String, ByVal strText As String) As XmlElement
' Insert a text node as a child of xNode.
' Set the tag to be strTag, and the
' text to be strText. Return a pointer
' to the new node.

Dim xNodeTemp As XmlNode

xNodeTemp = xDoc.CreateElement(strTag)
xNodeTemp.AppendChild(xDoc.CreateTextNode(strText))
xNode.AppendChild(xNodeTemp)

Return CType(xNodeTemp, XmlElement)
End Function

Private Sub UPDATE_SERVER_CFG()
Dim xDoc As New XmlDocument
xDoc.Load("C:\CEMDAS\DATA\SERVER_CONFIG.xml")

Dim xnode_SNAME As XmlNodeList
Dim mstrSERVERNAME As XmlNode
xnode_SNAME = xDoc.SelectNodes("//SERVER_NAME")

For Each mstrSERVERNAME In xnode_SNAME
If mstrSERVERNAME.InnerText = Trim(cboServerName.SelectedItem) Then
mstrSERVERNAME.InnerText = txtServerName.Text
mstrSERVERNAME.NextSibling.InnerText = txtIPADDRESS.Text
mstrSERVERNAME.NextSibling.NextSibling.InnerText = txtSERVER_DESC.Text
End If
Next
xDoc.Save("C:\CEMDAS\DATA\SERVER_CONFIG.xml")
CLEAR()
cboServerName.Visible = False
txtServerName.Visible = True
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
UPDATE_SERVER_CFG()
MsgBox("UPDATE Successfully !", MsgBoxStyle.Information)
End Sub

No comments:

Post a Comment