Medo Medo
Medo Medo

Reputation: 1018

Updating XML Tags Attributes Values in .NET

I have an xml string like that

<root>
Am trying <br id="9"/>to reorder the <br id="5"/>break 
lines <br id="10"/> attributes value
</root>

Any Way to change attribute value of XML BR tag ID attribute to be in sequence like this

<root>
Am trying <br id="1"/>to reorder the <br id="2"/>break 
lines <br id="3"/> attributes value
</root>

Upvotes: 1

Views: 400

Answers (1)

Garett
Garett

Reputation: 16828

Here is one example using LINQ TO XML

Dim doc as XElement = <root>
Am trying <br id="9"/>to reorder the <br id="5"/>break 
lines <br id="10"/> attributes value
</root>

Dim index as Integer = 0

For Each br In doc.<br>
    index += 1
    br.@id = index
Next 

This results in the following output

<root>
Am trying <br id="1" />to reorder the <br id="2" />break 
lines <br id="3" /> attributes value
</root>

Also, here's an example using a LAMBDA expression.

doc.<br>.ToList().ForEach(Sub(br) 
                index += 1 
                br.@id = index 
              End Sub)

Upvotes: 1

Related Questions