search XML file and replace certain values

drpcken
08-02-2004, 03:18 PM
Hey I'm learning to manipulate XML inside my VB.NET web application. So far I've figured out how to load the xml file into a dataset dataset.readxml and make changes to it per row and item name:

DS.Tables(0).Rows(0).Item("Name") = "NEW VALUE"

and then Dataset.writexml and rewrite this file with the new changes.

But I need to be able to search the xml file, whether it be in the dataset or not, and make changes to certain strings that are in there, and then save.

I'll have certain strings in my xml document that will have to be replaced with values I will be pulling into my application. Problem is how I will tell my application to search through the xml file and find and then replace the values I want.

Any help in the right direction woudl be greatly appreciated.

Thanks!

reboot
08-03-2004, 12:04 PM
'assuming xd is a previously opened xml document
'search for a node
Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
"/configuration/appSettings/add[@key=""" & _
key & """]"), Xml.XmlElement)


'change it
Node.Attributes.GetNamedItem("value").Value = value

drpcken
08-03-2004, 01:36 PM
hey thanks for replying!

But would xd be the dataset I loaded??

thanks!

drpcken
08-03-2004, 01:49 PM
Doing it that way... how do I load my xml file? because I'm trying by loading it into a dataset, and also as a xpathdocument but neither one will let me use

CType(xd.DocumentElement.SelectSingleNode

??

Thanks!

reboot
08-03-2004, 04:25 PM
'xml document object
Dim xd As New Xml.XmlDocument

'load an xml file
xd.Load(FileName)

'query for a value
Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
"/configuration/appSettings/add[@key=""" & key & """]")

'return the value
If Not Node Is Nothing Then
Return Node.Attributes.GetNamedItem("value").Value
End If

'search for a node
Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
"/configuration/appSettings/add[@key=""" & _
key & """]"), Xml.XmlElement)

'change it
Node.Attributes.GetNamedItem("value").Value = value

'save the file
xd.Save(FileName)

drpcken
08-03-2004, 06:13 PM
Thank you again for replying!

To use the above, I have to know which node the value I'm searching for is in correct?

reboot
08-03-2004, 08:05 PM
Yes

drpcken
08-03-2004, 10:21 PM
Is there a way to get the data to change without knowing which node it is in?

Thanks again!!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum