please see tutorial1 then read this tutorial.
http://www.dreaminco...topic142797.htm
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 inParentelement.AppendChild(xnode) 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
<Column-Name>Column-Value</Column-Name>
If you want to add Element without value means
<Column-Name>
</Column-Name>
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) inParentelement.AppendChild(xnode) 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) xnode.Attributes.Append(xattr) inParentelement.AppendChild(xnode) 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.





MultiQuote


|