Insert XML element after last concrete node / element

I want to add elements after the last child element in XML. But I have a mistake.

<XML ID="Microsoft Search Thesaurus"> <thesaurus xmlns="x-schema:tsSchema.xml"> <diacritics_sensitive>0</diacritics_sensitive> <expansion> <sub>Internet Explorer</sub> <sub>IE</sub> <sub>IE5</sub> </expansion> <replacement> <pat>NT5</pat> <pat>W2K</pat> <sub>Windows 2000</sub> </replacement> <expansion> <sub>run</sub> <sub>jog</sub> </expansion> <expansion> <sub>home</sub> <sub>house</sub> </expansion> </thesaurus> </XML> 

My code is below and I encounter this error when I debug the code.

InvalidOperationException was unhandled.

The sequence contains no elements.

 XDocument doc = XDocument.Load("tseng.xml"); //load the xml file. IEnumerable<XElement> MemberList = doc.Element("XML").Elements("thesaurus"); var Member = new XElement("expansion", new XElement("sub", "home"), new XElement("sub", "house") ); MemberList.Last().AddAfterSelf(Member); //add node to the last element. doc.Save("tseng.xml"); 

This error is for a specific line:

 MemberList.Last().AddAfterSelf(Member); 

I do not know what my problem is in this code. How to add an element after the last <expansion> node in my xml file?

Can you help me in solving this problem? Thanks.

+4
source share
2 answers

The <thesaurus> element has an xmlns attribute that indicates the namespace, so you need to add a namespace to all element names. In addition, AddAfterSelf should be used instead of AddAfterSelf if you want the new <expansion> element to appear inside the <thesaurus> , and not after it.

 XNamespace ns = "x-schema:tsSchema.xml"; IEnumerable<XElement> MemberList = doc.Element("XML").Elements(ns + "thesaurus"); var Member = new XElement(ns + "expansion", new XElement(ns + "sub", "home"), new XElement(ns + "sub", "house")); MemberList.Last().Add(Member); //add node to the last element. 
+6
source

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> <!-- elided: new element goes here --> <expansion> <sub>home</sub> <sub>house</sub> </expansion> </thesaurus> </XML> 
0
source

Source: https://habr.com/ru/post/1392358/


All Articles