尝试在 XmlNodeList 上使用 RemoveChild() 会弄乱我的 XmlNode 集合

发布于 2024-08-26 21:46:34 字数 1212 浏览 6 评论 0原文

我正在尝试从名为 listaWidths 的 XmlNodeList 中删除特定节点。在我使用RemoveChild()之前,这个特定列表有5个项目。但是,在RemoveChild()语句之后,列表仅保留1个项目。

XmlNodeList listaWidths = xmlDoc.SelectNodes("/MsBuild:Report/MsBuild:Body/MsBuild:ReportItems/MsBuild:Tablix/MsBuild:TablixBody/MsBuild:TablixColumns/*", nsmgr);                
int indexEpoca = 0;
XmlNode node = listaWidths[indexEpoca];
XmlNode parent = listaWidths[indexEpoca].ParentNode;
parent.RemoveChild(node);

这是 RDL 报告服务 XML。 具体的 XML 代码在这里:

  <Tablix Name="Tablix3">
    <TablixBody>
      <TablixColumns>
        <TablixColumn>
          <Width>1.602in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.61in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.6323in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.6023in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.6033in</Width>
        </TablixColumn>
      </TablixColumns>
      (...)

我已经尝试了所有可能的组合,但没有任何运气。我做错了什么? 谢谢。

I'm trying to remove a specific node from a XmlNodeList named listaWidths. This specific list has 5 items before I use RemoveChild(). But, after the RemoveChild() statement, the list stays only with 1 item.

XmlNodeList listaWidths = xmlDoc.SelectNodes("/MsBuild:Report/MsBuild:Body/MsBuild:ReportItems/MsBuild:Tablix/MsBuild:TablixBody/MsBuild:TablixColumns/*", nsmgr);                
int indexEpoca = 0;
XmlNode node = listaWidths[indexEpoca];
XmlNode parent = listaWidths[indexEpoca].ParentNode;
parent.RemoveChild(node);

This is a RDL Reporting Services XML.
The specific XML code is here:

  <Tablix Name="Tablix3">
    <TablixBody>
      <TablixColumns>
        <TablixColumn>
          <Width>1.602in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.61in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.6323in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.6023in</Width>
        </TablixColumn>
        <TablixColumn>
          <Width>1.6033in</Width>
        </TablixColumn>
      </TablixColumns>
      (...)

I've tried every combination possible, with no luck whatsoever. What am I doing wrong?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

鸢与 2024-09-02 21:46:34

SelectNodes 的文档明确指出:“此返回的 XmlNodeList 对象方法将在底层文档保持不变的情况下有效,如果底层文档发生更改,可能会返回意外结果(不会抛出异常)。”

因此,在操作文档后,您对从 SelectNodes 返回的 XmlNodeList 的体验可能不是您所期望的,但与文档一致。如果您操作文档,则必须再次调用 SelectNodes 才能获取新的 XmlNodeList。

在我看来,.NET 框架中 DOM 实现的设计者犯了一个错误,他们使用 XmlNodeList 作为具有完全不同行为的具体实现的抽象类。如果您使用例如 ChildNodes,那么您将获得符合 W3C DOM 规范要求的“实时”节点列表,因此在这种情况下,文档更改会自动更改节点列表(如果需要)。然而,正如您所经历的那样,SelectNodes 返回一个具有完全不同行为的节点列表。

The documentation of SelectNodes clearly says: "The XmlNodeList object returned by this method will be valid while the underlying document remains unchanged. If the underlying document changes, unexpected results may be returned (no exception will be thrown)."

So what you experience with your XmlNodeList returned from SelectNodes after you manipulate the document might not be what you expect but is in line with the documentation. You will have to call SelectNodes again to get a new XmlNodeList if you manipulate the document.

In my view the designers of the DOM implementation in the .NET framework made a mistake by using XmlNodeList as an abstract class for concrete implementation with quite different behaviour. If you use e.g. ChildNodes then you get a "live" node list in line with what the W3C DOM specification requires so in that case a document change automatically changes the node list (if needed). However SelectNodes returns a node list with a quite different behaviour, as you experienced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文