I’m trying to use linq to find MyNode where the name equals Foo, and make a copy of that node and add it to the XML but the new node should have name Bar, and then save the file.
<?xml version="1.0" encoding="utf-8"?>
<MyRoot>
<MyNode Name="Foo">
<Data Type="String">ABC</Data>
</MyNode>
</MyRoot>
this code finds the node
Dim doc As XDocument = XDocument.Load(xmlFile)
Dim sheet = From item In doc...<MyRoot>...<MyNode> Select item Where item.@Name = "Foo"
and I’m trying to use this to add the node and change the name.
sheet.@Name = "Bar" 'After excecuting this, sheet becomes "Nothing"
doc.Root.Add(sheet.First)
doc.Save(outFile)
however, after the setting sheet.@Name to “Bar”, then sheet becomes Nothing. If I comment out that line, the output will have two nodes, both named Bar. I suspect I am not doing this the “right way”, in either of terms of changing the attribute or adding this to the XDocument (or both)
c# version (will try to translate)
should be in vb