Ravo86
Ravo86

Reputation: 55

Add attributes with XElement

I have a file xml like this:

<?xml version="1.0" encoding="utf-8"?>

<svg version="1.1">
<g ID="Prova1"/>
<g ID="Prova2"/>
</svg>

I need to add some attributes in the nodes For example:

<?xml version="1.0" encoding="utf-8"?>

<svg version="1.1">
 <g ID="Prova1" onclick="prova()" />
 <g ID="Prova2" onclick="prova()" />
</svg>

Where the id is null i don't do something. I use VB.net and class XElement

Upvotes: 0

Views: 340

Answers (2)

dbasnett
dbasnett

Reputation: 11773

Give this a try

Dim someXE As XElement = <svg version="1.1">
                             <g ID="Prova1"/>
                             <g ID="Prova2"/>
                         </svg>

For Each xe As XElement In someXE.Elements
    xe.@onclick = "prova()"
Next

Upvotes: 1

jdweng
jdweng

Reputation: 34421

Try this

Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Dim xml As String = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
                            "<svg version=""1.1"">" & _
                            "<g ID=""Prova1""/>" & _
                            "<g ID=""Prova2""/>" & _
                            "</svg>"
        Dim element As XElement = XElement.Parse(xml)
        For Each g As XElement In element.Descendants("g")
            g.Add(New XAttribute("onclick", "prova()"))
        Next g
    End Sub

End Module
​

Upvotes: 0

Related Questions