Page 1 of 1

Working of xml in vb.net with examples this tutorial tells you how to use xml in vb.net Rate Topic: ***** 1 Votes

#1 theunborncoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 123
  • Joined: 15-October 09

Post icon  Posted 02 December 2009 - 12:07 AM

XML basically used for store and transport data.

first of all you have to import xml file
Imports System.Xml


xml file can be read in 2 ways
1.using xmldocument
2.using xmlreader Class
here we using first approach that is using xmldocument. so create an object of xmldocument
 Dim xmldoc As New XmlDocument


we require xmlelement to refer to element in xml so create a variable of xmlelement
 Dim myelement As XmlElement



first we have to load an xml file
dot net provides load function to load xml file here i am writing a load function
		''' <summary>
		''' Give Full path to load Xml file into memory
		''' </summary>
		''' <param name="path">It Contains Full Path</param>
		''' <remarks></remarks>
		Public Sub load(ByVal path As String)
			xmldoc.Load(path)
		End Sub


now our xmlfile is loaded into xmldoc
now we want to access to particular node in xml file so i am writing a one function which accepts path and returns you a element
		''' <summary>
		''' Function Returns Parent Element
		''' </summary>
		''' <param name="Path">It /Separated Path Ex. /root/child ['subchild=value']</param>
		''' <returns></returns>
		''' <remarks></remarks>
		''' 
		Public Shared Function Return_XML_Element(ByVal XMLDOC As Xml.XmlDocument, ByVal Path As String) As XmlElement
			Dim node As XmlNode
			Dim xnode As XmlElement
			node = XMLDOC.SelectSingleNode(Path)
			xnode = CType(node, XmlElement)
			Return xnode

		End Function



because of above function you can access node of xml. if you want to access value of that node. i am writing another function which accepts xmlelement as parameter which u can get from above function and name of element
		''' <summary>
		''' Function Returns you Inner Text of Element
		''' </summary>
		''' <param name="inParentElement">Accept XmlElement as Parameter</param>
		''' <param name="columnName">Accept Element Name Which You want to Find Out Inner Text</param>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Shared Function Return_Column_Value(ByVal inParentElement As XmlElement, ByVal columnName As String) As String
			Dim i As Integer

			If inParentElement.HasChildNodes Then
				For i = 0 To inParentElement.ChildNodes.Count
					If Trim(UCase(inParentElement.ChildNodes(i).Name)) = Trim(UCase(columnName)) Then
						Return inParentElement.ChildNodes(i).InnerText
					End If
				Next
			End If
			Return ""
		End Function



you can change value of that element also just you have to add 2-3 lines in above code. In above function accept 1 more parameter for new value
say inNewInnerText and write 1 statement before return statement
inParentElement.ChildNodes(i).InnerText = inNewInnerText


i am showing you in separate function as follows
		''' <summary>
		''' Function Sets Inner Value of Element
		''' </summary>
		''' <param name="inParentElement">Accept XmlElement as Parameter</param>
		''' <param name="columnName">Accept Element Name Which You want to Change Inner Text</param>
		''' <param name="inNewInnerText">New Inner Text</param>
		''' <remarks></remarks>
		Public Sub Set_Value(ByVal inParentElement As XmlElement, ByVal columnName As String, ByVal inNewInnerText As String)
			Dim i As Integer

			If inParentElement.HasChildNodes Then
				For i = 0 To inParentElement.ChildNodes.Count
					If Trim(UCase(inParentElement.ChildNodes(i).Name)) = Trim(UCase(columnName)) Then
						inParentElement.ChildNodes(i).InnerText = inNewInnerText
						Exit For
					End If
				Next
			End If
		End Sub



until now we have seen how to load xml file and access a particular node & change its value.
now see for how to save changes to xml file
	   ''' <summary>
		''' Function is used to save changes in xml file
		''' </summary>
		''' <param name="SaveFilepath">Give Full path of </param>
		''' <remarks></remarks>
		Public Sub save(ByVal SaveFilepath As String)
			xmldoc.Save(SaveFilepath)
		End Sub


in above function i am accepting path from user that is our xml file path
in next tutorial i will add some more functionality

Thank you.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1