Please note i am new to LINQ to XML.
I am now trying to add A node(descendant) TO a Node
eg.
From this:
<World>
<Country Name = "South_Africa"/>
</World>
To This:
<World>
<Country Name = "South_Africa">
<Person Name = "Aiden Strydom"/>
</Country>
</World>
As you can see i am trying to add a Person node to Country,
but i am not succeeding
Code:
' Load current document.
Dim XMLDoc As XDocument = XDocument.Load("Test.xml")
' Make new XElement based on incoming parameters.
Dim MyElement As XElement = _
<Person Name=<%= PersonName %>/>
' Find The Node in Question
Dim e As IEnumerable(Of XElement) = From element
In XMLDoc.Root.Elements("Country")
Where element.Attribute("Name").Value = "South_Africa"
Select element
' Add To it
e.FirstOrDefault().AddFirst(MyElement)
' Save changes to disk.
XMLDoc.Save("Test.xml")
You didn’t specify what the actual issue is, but you appear to be mixing script markup with code:
As well as not creating the XElement properly. I’d try this:
This will take the input XML:
And generate:
Assuming PersonName in the code above = “Aiden Strydom”.