You need to explicitly specify the namespace for thesaurus element, since it is different from the name for root XML node:
XNamespace ns = "x-schema:tsSchema.xml"; XDocument doc = XDocument.Load("tseng.xml"); IEnumerable<XElement> MemberList = doc.Element("XML").Elements(ns + "thesaurus");
Also, remember that your current code will add a new expansion element after thesaurus one, and not its children. If you want to add a new expansion element under the last thesaurus element, you should use this code instead:
var MemberList = root.Elements(ns + "thesaurus"); var Member = new XElement(ns + "expansion", new XElement(ns + "sub", "home"), new XElement(ns + "sub", "house") ); MemberList.Last().Add(Member);
This will produce the XML as shown below:
<XML ID="Microsoft Search Thesaurus"> <thesaurus xmlns="x-schema:tsSchema.xml"> <diacritics_sensitive>0</diacritics_sensitive> <expansion> <sub>home</sub> <sub>house</sub> </expansion> </thesaurus> </XML>
source share