Hi,
i am doing a VB.NET Windows Application. In that i am trying to select a node from XML File and add child nodes to that selected node. My XML File looks like this
Here i want to select the node whose CardCode is CC6 and Version is 1.0.
How i should write the code for this .. Also after selecting that i want to create a child node for that MF with attributes BS, FI, AC, KN etc. Can anybody help me in doing this .. plz
AlanPosted Aug 7, 2008, 6:36 AM
Try this code. For now, I've just added the new 'MF' node after the existing one:
Imports System
Imports System.Xml
Module Program
Sub Main()
Dim doc As New XmlDocument
doc.Load("oca.xml") ' say
Dim list As XmlNodeList = doc.GetElementsByTagName("CardCode")
Dim csNode As XmlNode = Nothing
For Each ccNode AS XmlNode in list
Dim value As String = ccNode.InnerText
If value = "CC6" Then
csNode = ccNode.ParentNode
Dim vNode As XmlNode = csNode.SelectSingleNode("Version")
If vNode IsNot Nothing Then
value = vNode.InnerText
If value = "1.0" Then
Exit For
End If
End If
End If
Next
If csNode IsNot Nothing Then
Dim mfNode As XmlElement = doc.CreateElement("MF")
'add some attributes with arbitrary values
mfNode.SetAttribute("BS", "3821")
mfNode.SetAttribute("FID", "3F01")
mfNode.SetAttribute("AC", "0001")
mfNode.SetAttribute("KN", "0001")
csNode.AppendChild(mfNode)
Console.WriteLine("Saving amended file")
doc.Save ("oca2.xml") ' use same filename if want to overwrite
End If
End Sub
End Module