Page 1 of 1

Working of xml in with examples Part-2 this tutorial tells you how to use xml in Rate Topic: -----

#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 05 December 2009 - 07:22 AM

please see tutorial1 then read this tutorial.


In previous tutorial we have seen how to load xml file & access its node , How to save file.

In this tutorial we see How to add Elements, Elements with attributes & how to remove Elements
If you remember i am creating an instance of xmldocument in previous tutorials I am using same instance in this tutorial also
Dim xmldoc As New XmlDocument

You can combine both tutorial code into one single class & use it wherever you want.

in previous tutorial we have seen how to access particular node. thaat is Return_XML_Element function. this function return you parent element .
Now we have to add child node under that parent node so i am writnig a function for that
	  ''' <summary>
		''' Function Add New child to Column
		''' </summary>
		''' <param name="inParentelement">Accept XmlElement as Parameter</param>
		''' <param name="ColumnName">Accept Element Name</param>
		''' <param name="ColumnValue">Accept Element Value</param>
		''' <remarks></remarks>
		Public Sub New_Child(ByVal inParentelement As XmlElement, ByVal ColumnName As String, ByVal ColumnValue As String)
			Dim xnode As XmlNode
			xnode = xmldoc.CreateElement(ColumnName)
			xnode.InnerText = ColumnValue
		End Sub

Return_XML_Element returns you parent element. Pass that element to New_Child function and Column Name which u want to add & its Value
it look likes

If you want to add Element without value means
then u can do in following way
 ''' <summary>
		''' Function is used to add childnode under parent node
		''' </summary>
		''' <param name="inParentelement">Accepts parent Element</param>
		''' <param name="Column-Name">Accept Child Node Name</param>
		''' <remarks></remarks>
		Public Sub Add_Column(ByVal inParentelement As XmlElement, ByVal Column-Name As String)
			Dim xnode As XmlNode
			xnode = xmldoc.CreateElement(Column-Name)
		End Sub

If you want to add element with its attribute then u have to add 2-3 lines more in existing function or you can write separate function for it.
		''' <summary>
		''' Add New ChildNode with its Attributes under Parent 
		''' </summary>
		''' <param name="inParentelement">Accept XmlElement as Parameter</param>
		''' <param name="Column_name">Accept Column Name</param>
		''' <param name="Attribute_Name">Accept Column Attribute Name</param>
		''' <param name="Attribute_value">Accept Column Attribute Value</param>
		''' <remarks></remarks>
		Public Sub Add_Column(ByVal inParentelement As XmlElement, ByVal Column_name As String, ByVal Attribute_Name As String, ByVal Attribute_value As String)
			Dim xnode As XmlNode
			Dim xattr As XmlAttribute

			xattr = xmldoc.CreateAttribute(Attribute_Name)
			xattr.Value = Attribute_value
			xnode = xmldoc.CreateElement(Column_name)
		End Sub

if you use this function to add element in xml the node will look like this
<Column-Name Attribute_Name="Attribute_value">
above function contains 4 parameter 1st is xmlElement 2nd name of column which u want to add 3rd is attribute name & 4th is the value of attribute

Now we want to remove a node from xml file
for that we have to access that which u want to remove.
so using Return_XML_Element function u will get particular which u want to delete
		''' <summary>
		''' Remove Element from Xml File.
		''' </summary>
		''' <param name="inelement">Accept Element which you want to delete.</param>
		''' <remarks></remarks>
		Public Sub Remove(ByVal inelement As XmlElement)
			inelement.ParentNode.RemoveChild(inelement )

when you do changes then call save method to reflect changes in Xml file
Thank You.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1