Reputation: 1875
I have an XML document with the following structure:
<position index="x">
<character>y</character>
</position>
I need to be able to add a new character to a position based on its index. For example, where index = "3", add the character "g".
I know I can find an element with the following:
var query = from positions in myDoc.Descendants("position")
where (string)positions.Attribute("index").Value == n
select positions;
But I'm having trouble figuring out if I need a similar kind of query or construction to identify an element with attribute value x, then add child nodes.
Upvotes: 1
Views: 459
Reputation: 31444
Your query already returns elements you want to add to, so it boils down to:
var query = from positions in myDoc.Descendants("position")
where (string)positions.Attribute("index").Value == n
select positions;
foreach (var position in query)
{
position.Add(new XElement("character", "g"));
}
Upvotes: 1