1

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>

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

doc.Elements("br") could be simplified to doc.<br>.
@MarkHurd good point. I'll update the sample to reflect this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.