I have xml that looks like this. I’m been stuck for a while on trying to add more values to the Author element.
I load it in like so:
XDocument cdata = XDocument.Parse(text);
here is the sample xml:
<a xmlns="http://www" version="5.050">
<Books>
<Author> Poe,Edgar Allen Homer Walden</Author>
<Address></Address>
<State></State>
</Books>
<SYSTEM type="OTHER">
<ORGANIZATION id="" />
</SYSTEM>
<HMDA_INFO is_hoepa="N" is_hoepa_manual="N" />
</a>
Just a side note ‘a’ is not necessarily always the root element name sometimes its b or c so I can’t go by the root name.
Tried doing this to access Author but I get sequence contains no elements:
XDocument cdata = XDocument.Parse(text);
var addElement = cdata.Descendants("Books").First();
addElement.SetAttributeValue("Authors", "insert this.");
You have two problems. First is that
Authoris an element, not attribute. And second – you forgot about namespace declared inaelement:Or if you want just to add new attribute to authors element: