Inserting into a XML file VB.net

Saturday, June 27, 2015 Unknown 1 Comments


Function InsertXML(ByVal filein As String, ByVal fullpath As String, ByVal x As Integer)
'************************Insert data to xml file************************
' // Create xml dom
Dim XMLDOM As New XmlDocument()
' //load your xml file
XMLDOM.Load("config.xml")
'//Select main node
Dim newXMLNode As XmlNode = XMLDOM.SelectSingleNode("CONFIG/DIRECTORIES")
' //get the node where you want to insert the data
Dim childNode As XmlNode = XMLDOM.CreateNode(XmlNodeType.Element, "PATH", "")
'
childNode.InnerText = fullpath + "," + x.ToString
newXMLNode.AppendChild(childNode)
XMLDOM.Save("config.xml")
Return True
End Function


The above function allows you to insert into a node and place the text value in the xml file.
ie. Books - Author [Stephen King] would look like

unction InsertXML(ByVal filein As String, ByVal fullpath As String, ByVal x As Integer)
'************************Insert data to xml file************************
' // Create xml dom
Dim XMLDOM As New XmlDocument()
' //load your xml file
XMLDOM.Load("config.xml")
'//Select main node
Dim newXMLNode As XmlNode = XMLDOM.SelectSingleNode("BOOKS")
' //get the node where you want to insert the data
Dim childNode As XmlNode = XMLDOM.CreateNode(XmlNodeType.Element, "AUTHOR", "")
'
childNode.InnerText = "Stephen King"

newXMLNode.AppendChild(childNode)
XMLDOM.Save("config.xml")
Return True
End Function


Config.xml is the xml file to load.

1 comment :

Powered by Blogger.