question

Havooma-1991 avatar image
0 Votes"
Havooma-1991 asked DewayneBasnett-7583 answered

Visual Basic how to update an xml attribute

I have searched everywhere but cant find something that works! I have some xml which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<times>
<given type="type1">
<data1>"data1 here</data1>
<data2>"data2 here"</data2>
</given>
<given type = "type2">
<data1>"data1 here</data1>
<data2>"data2 here"</data2>
</given>
<given type = "type3">
<data1>"data1 here</data1>
<data2>"data2 here"</data2>
</given>
</times>

I want to change the name of "type2" to "typeX". I have some code below but it doesnt work. Any ideas would be most welcome!

  Dim newValue As String
  Dim xmlDoc = New XmlDocument
     
 xmlDoc.Load(xmlfile)
    
  Dim attr As XmlAttribute
    
  attr = xmlDoc.SelectSingleNode("/given[@type='type2']")
  attr.Value = "typeX"
    
  xmlDoc.Save(xmlfile)
dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Havooma-1991 avatar image
0 Votes"
Havooma-1991 answered

So I worked it out after all! Here is the solutions:

         Dim xmlDoc = New XmlDocument
         xmlDoc.Load(xmlfile)
    
    
         Dim attr As XmlAttribute = DirectCast(xmlDoc.SelectSingleNode(source_attribute_path), XmlAttribute)
         attr.Value = new_attribute_name
    
         xmlDoc.Save(xmlfile)

Where the source_attribution_path is

"//times/given[@type='type2']/@type"

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DewayneBasnett-7583 avatar image
0 Votes"
DewayneBasnett-7583 answered

Another approach using XElement and LINQ

         Dim xmlFile As XElement
    
         xmlFile = XElement.Load("path here")
         'for testing
         xmlFile = <times>
                       <given type="type1">
                           <data1>"data1 here</data1>
                           <data2>"data2 here"</data2>
                       </given>
                       <given type="type2">
                           <data1>"data1 here</data1>
                           <data2>"data2 here"</data2>
                       </given>
                       <given type="type3">
                           <data1>"data1 here</data1>
                           <data2>"data2 here"</data2>
                       </given>
                   </times>
    
         Dim xelND As XElement
         xelND = (From el In xmlFile.<given>
                    Where el.@type = "type2"
                    Select el
                    Take 1).FirstOrDefault
    
         If xelND IsNot Nothing Then xelND.@type = "typeX"
    
         xmlFile.Save("path here")
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.