在最后一个特定节点/元素之后插入 XML 元素

发布于 2024-12-28 14:32:51 字数 1573 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2025-01-04 14:32:51

元素具有指定命名空间的 xmlns 属性,因此您需要将命名空间添加到所有元素名称中。另外,如果您希望新的 元素出现在 内部,则应使用 Add 而不是 AddAfterSelf 而不是在它之后。

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. 

The <thesaurus> element has an xmlns attribute that specifies a namespace, so you need to add the namespace to all the element names. Also, you should use Add instead of AddAfterSelf if you want the new <expansion> element to appear inside <thesaurus> instead of 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. 
云淡风轻 2025-01-04 14:32:51

您需要为 thesaurus 元素显式指定命名空间,因为它与根 XML 节点的命名空间不同:

XNamespace ns = "x-schema:tsSchema.xml";
XDocument doc = XDocument.Load("tseng.xml");
IEnumerable<XElement> MemberList = doc.Element("XML").Elements(ns + "thesaurus");

另外,请记住您当前的代码将添加新的 扩展 元素位于 同义词库 元素之后,而不是其子元素。如果您想在最后一个 thesaurus 元素下添加新的 expansion 元素,您应该使用以下代码:

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);

这将生成类似于以下示例的 XML:

<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>

You need to specify namespace explicitly for thesaurus element, as it is different than the one 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, keep in mind your current code will add new expansion element after thesaurus one, not as its children. If you want to add 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 XML similar to example 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文