[quote=412946:@Beatrix Willius]xml = Replace(xml, "<HostName>OV</HostName>", "<HostName>OV02</HostName>")
If your change is really simple, then you can use a simple solution like my code above. If you need a more complex change or the xml changes then you have to find a different solution.[/quote]
I’ve tried your solution, but when I want to run the program it gives me an error:
"There is more than one item with this name and it’s not clear to which this refers to
What am I doing wrong?
[quote=412948:@Paul Lefebvre]First you want to make sure you XML is really XML. So it needs to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<Monitor>
<DeviceInfo>
<DeviceName>UNIT</DeviceName>
<HostName>OV</HostName>
<ID>862462032347916</ID>
<FwVer>v1.10</FwVer>
<MnfInfo>myunit.example</MnfInfo>
</DeviceInfo>
</Monitor>
I put the above in a constant called kXML.
Then you can parse it to get the XmlNode that points to what you want to change. One way is to directly refer to it like this:
[code]Dim xml As New XmlDocument
xml.LoadXml(kXML)
// Get
Dim deviceInfo As XmlNode
// xml.Firstchild is and its FirstChild is
deviceInfo = xml.FirstChild.FirstChild
// Get
// This is the 2nd (0-based so use 1) child of
Dim hostName As XmlNode
hostName = deviceInfo.Child(1)
// Get value for 's child which is OV
Dim hostNameValue As String = hostName.FirstChild.Value
// Change value for 's child
hostName.FirstChild.Value = “OV02”
// The updated XML which you can use, save, etc.
Dim newXML As String = xml.ToString
[/code]
Alternatively you could search directly for the node you want and then change it. You would do that using Xql:
[code]Dim xml As New XmlDocument
xml.LoadXml(kXML)
// Search entire XML document for anything that contains and
// get the results back as a list.
Dim hostNames As XmlNodeList
hostNames = xml.Xql("//HostName")
If hostNames.Length >= 0 Then
// At least one was found so change the first one’s value
// Change value for
hostNames.Item(0).FirstChild.Value = “OV02”
End If
Dim newXML As String = xml.ToString[/code]
For reference:
OK. I’ve tried the first solution you gave me, but with no luck. I will give a more direct example on something I’m working on