在最后一个特定节点/元素之后插入 XML 元素
我想在 XML 中的最后一个子项之后添加元素。但我得到了一个错误。
<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>
我的代码如下,当我调试代码时遇到此错误。
InvalidOperationException 未处理。
<块引用>序列不包含元素。
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");
此错误针对特定行:
MemberList.Last().AddAfterSelf(Member);
我不知道这段代码有什么问题。如何将最后一个
节点后面的元素添加到 XML 文件中?
你能帮我解决这个问题吗?谢谢。
I want to add elements after last child in the XML. But I got an error.
<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.
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 specific line:
MemberList.Last().AddAfterSelf(Member);
I don't know what is my problem on this code. How can I add element after last <expansion>
node to my XML file?
Can you help me for this problem? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
元素具有指定命名空间的xmlns
属性,因此您需要将命名空间添加到所有元素名称中。另外,如果您希望新的
元素出现在内部,则应使用
而不是在它之后。Add
而不是AddAfterSelf
。The
<thesaurus>
element has anxmlns
attribute that specifies a namespace, so you need to add the namespace to all the element names. Also, you should useAdd
instead ofAddAfterSelf
if you want the new<expansion>
element to appear inside<thesaurus>
instead of after it.您需要为
thesaurus
元素显式指定命名空间,因为它与根XML
节点的命名空间不同:另外,请记住您当前的代码将添加新的
扩展
元素位于同义词库
元素之后,而不是其子元素。如果您想在最后一个thesaurus
元素下添加新的expansion
元素,您应该使用以下代码:这将生成类似于以下示例的 XML:
You need to specify namespace explicitly for
thesaurus
element, as it is different than the one for rootXML
node:Also, keep in mind your current code will add new
expansion
element afterthesaurus
one, not as its children. If you want to add newexpansion
element under the lastthesaurus
element, you should use this code instead:This will produce XML similar to example below: