Answers to similar questions aren't helping me; or I'm missing something very obvious.
I want to add a <value> element into a <question> element in XML structured like this:
<form>
<foo>
<bar>
<question id="1">...</question>
<question id="2">...</question>
</bar>
</foo>
</form>
Other questions seem to focus on adding an element into the root element but I'm trying to add mine further into the tree based on attribute values. I have tried the following which doesn't work:
XDocument newFormTemplateXML = XDocument.Load("newFormTemplate.xml");
XElement newValue = new XElement("value", 123);
newFormTemplateXML
.Descendants()
.Where(d => d.Name.ToString().Equals("question") && d.Attribute("id").Equals(1))
.Append(newValue);
newFormTemplateXML.Save("test.xml");
I'm not getting an error message. Can someone help me on the right path please?
newFormTemplateXML.Root.Add(newValue)newFormTemplateXML.Descendants("question").Where(q => (int) q.Attribute("id") == 1).Single().Add(new XElement("boo"))works for me.