VB.NET XML Search, Read, Write.

coleman01

Member
Joined
Apr 8, 2005
Messages
11
Programming Experience
Beginner
Hi,
Im trying to search through an XML file. When it finds the record it needs it then updates a specific field in that record. I have made it find the specified record but do not know how to update it and write it back to file. I am new to XML coding. Any help would be most appreciated.

Dim xpathDoc As XPathDocument
Dim xmlNav As XPathNavigator
Dim xmlNI As XPathNodeIterator
Dim filePath As New OpenFileDialog

filePath.ShowDialog()
xpathDoc = New XPathDocument(filePath.FileName)
xmlNav = xpathDoc.CreateNavigator()
xmlNI = xmlNav.Select("/Message/Pupils/PupilCode")

While (xmlNI.MoveNext())
System.Console.WriteLine(xmlNI.Current.Name + " : " + xmlNI.Current.Value)

If (xmlNI.Current.Value) = "A123981456002" Then

'ONCE ITS FOUND THE RECORD - DISPLAY A MESSAGE
'SELECT THE DIFFERENT FIELD WITHIN THE RECORD
MsgBox("Found")
xmlNav.Select("/Message/Pupils/PupilAge")
'THIS IS WHERE IT NEEDS TO UPDATE IT. NO IDEA HOW TO DO THIS BIT
End If
End While


This may be the wrong way to do it - I'm not sure. I just need it to search for a specific pupil ID and update their age with a specified value. After that it has to write the entire XML file to a new file.

Many thanks.
Chris
 
You can do this by using a dataset. you can read an xml file into a dataset and then search the dataset for your field. once you have found it you can edit it and write the changes back to the xml file.

VB.NET:
 [size=2]
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ds [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataSet = [/size][size=2][color=#0000ff]New[/color][/size][size=2] DataSet()

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] dv [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataView

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] id [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff][size=2]ds.ReadXml(Server.MapPath([/size][size=2][color=#800000]"XMLFile.xml"[/color][/size][size=2]))

dv = ds.Tables(0).DefaultView

dv.Sort = [/size][size=2][color=#800000]"id"[/color][/size]
[size=2][color=#800000][size=2]id = dv.Find(TextBoxID.Text)
[size=2]ds.Tables(0).Rows(id).Item([/size][size=2][color=#800000]"Name"[/color][/size][size=2]) = TextBoxName.Text

ds.WriteXml(Server.MapPath([/size][size=2][color=#800000]"XMLFile.xml"[/color][/size][size=2]))

[/size][/size][/color][/size][/color][/size]
I hope that helps. If not let me know because i've just cut that out of my code.
 
Hi,

Thank you very much for your quick response. I thought about using a dataset but the xml file has nested relations with the same name and I just get an error ("the same table (tableName) cannot be the child table in two nested relations"). I cant really rename the other tables as it is a structured file that is set and cannot be realistically changed.

If you have any ideas that I can use to get around this I would be REALLY grateful.

Many thanks.
Chris
 
Back
Top