|
-
Mar 16th, 2016, 10:07 AM
#1
Thread Starter
Lively Member
Update specific attibute in XML file.
Hi, I'm trying to update an XML file, but only a specific row.
For instance in the xml sample below, there are four records, each with their own ID.
I want to be able to update the record, where the ID = 4
Code:
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<returnset>
<ID>3</ID>
<EJStatus />
</returnset>
<returnset>
<ID>4</ID>
<EJStatus />
</returnset>
<returnset>
<ID>5</ID>
<EJStatus />
</returnset>
<returnset>
<ID>6</ID>
<EJStatus />
</returnset>
</DocumentElement>
This is the code sample I've been working with, but this only updates the first instance of EJstatus.
Code:
Public Sub SaveXMLFile()
'Dim xmlDoc As New XmlDataDocumentxmlDoc.Load(dirPath & "jobs3.xml")
Dim xmlDoc As New XmlDataDocument()
xmlDoc.Load(dirPath & "jobs3.xml")
Dim node As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/ID[. ='4']")
Dim nodev As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/EJStatus")
If node IsNot Nothing Then
nodev.InnerText = "done"
End If
'For Each Attribute As XmlAttribute In xmlDoc.DocumentElement.Attributes
' If Attribute("ID").Value = 4 Then
' Attribute("EJStatus").InnerText = "read"
' End If
' Next
xmlDoc.Save(dirPath & "jobs3.xml")
End Sub
I also tried looping each attribute to find the correct record for updating, but this never worked either.
Any help on how to update the correct record would be much appreciated.
Many thanks
Dave
-
Mar 16th, 2016, 10:25 AM
#2
Re: Update specific attibute in XML file.
Code:
Dim node As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/ID[. ='4']")
Dim nodev As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/EJStatus")
It's updating the first one, because that's what you told it to update... that second selectsinglenode pulls the first instance of the EJStatus it finds... but it's starting relative to the root... not the node where ID =4 ...
what you need to do is start with the selected node (node) and find the sibling "EJStatus" ... which is easy enough because there's a NextSibling method...
so what you end up with should be this (untested)
Code:
Public Sub SaveXMLFile()
'Dim xmlDoc As New XmlDataDocumentxmlDoc.Load(dirPath & "jobs3.xml")
Dim xmlDoc As New XmlDataDocument()
xmlDoc.Load(dirPath & "jobs3.xml")
Dim node As XmlNode = xmlDoc.SelectSingleNode("//DocumentElement/returnset/ID[. ='4']")
Dim nodev As XmlNode = node.NextSibling()
If nodev IsNot Nothing Then 'It's more important that nodev isn't nothing than it is node...
nodev.InnerText = "done"
End If
xmlDoc.Save(dirPath & "jobs3.xml")
End Sub
This assumes that EJStatus is the next node following ID... if it isn't... you might need a small loop to check the .NextSibling until the node's name is the right one.
-tg
edit - your looping of the attributes didn't work because there are no attributes... those are all nodes...
-
Mar 16th, 2016, 11:01 AM
#3
Thread Starter
Lively Member
Re: Update specific attibute in XML file.
Thanks technome,
nextsibling was very handy,, I did have to loop to find the correct node and update it..
It works perfectly.
Thanks again.
Dave
-
Mar 17th, 2016, 07:43 AM
#4
Re: Update specific attibute in XML file.
As an alternative, using Framework 3.5 or higher
Code:
Dim Identifier As Integer = 4
Dim results = xmlDoc...<returnset> _
.FirstOrDefault(
Function(item) item.<ID>.Value = Identifier.ToString)
If results IsNot Nothing Then
results.<EJStatus>.Value = "Got it"
' show in the ide output window the element has changed
Console.WriteLine(xmlDoc.ToString)
End If
output
Code:
<DocumentElement>
<returnset>
<ID>3</ID>
<EJStatus />
</returnset>
<returnset>
<ID>4</ID>
<EJStatus>Got it</EJStatus>
</returnset>
<returnset>
<ID>5</ID>
<EJStatus />
</returnset>
<returnset>
<ID>6</ID>
<EJStatus />
</returnset>
</DocumentElement>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|