Reputation: 1962
Trying to add an xml element and attribute between an existing one. I have xml template that looks lik e this:
<INPUT>
<LOGIN user="cat" password="meow" />
<REC>
</REC>
</INPUT>
I'm looking to add an element and atribute between <REC></REC>
treid formattging soemthing like this but it sticks it after LOGIN not REC
IEnumerable<XElement> list = doc.Element("INPUT").Elements("LOGIN");
var addElement = new XElement("an", new XAttribute("id", i));
list.Last().AddAfterSelf(addElement);
Upvotes: 0
Views: 1016
Reputation: 1502376
It sounds like you're trying to add it as a child of REC
. That's easy:
// If there are multiple `REC` elements, you'll need to work out which one you want
var recElement = doc.Descendants("REC").First();
recElement.Add(new XElement("an", new XAttribute("id", i)));
Upvotes: 3