C# 使用 XPATH 选择具有已知值的特定元素,然后删除

发布于 2024-11-05 22:13:03 字数 1763 浏览 4 评论 0原文

我是 C# 中 xpath 的新手,我正在尝试选择一个具有两个特定值的元素。这就是 XML 格式的样子,

<?xml version="1.0" encoding="utf-8"?>
<Manager>
  <SSH>
    <Tunnels>
      <Port>
        <Local>443</Local>
        <Remote>443</Remote>
      </Port>
      <Port>
        <Local>5432</Local>
        <Remote>5432</Remote>
      </Port>
      <Port>
        <Local>19</Local>
        <Remote>21</Remote>
      </Port>
      <Port>
        <Local>19</Local>
        <Remote>22</Remote>
      </Port>
    </Tunnels>
  </SSH>
</Manager>

我试图选择一个具有先前表单中的值的“端口”,以便我可以从 xml 中删除该特定条目。这是我正在使用的代码:

        //remove children from selected
        XmlNode _xmlTunnel = _xml.SelectSingleNode("/Manager/SSH/Tunnels/Port[Local=" + _local + "] | /Manager/SSH/Tunnels/Port[Remote=" + _remote + "]");
        MessageBox.Show("Local " + sshList.SelectedItems[0].Text + " Remote " + sshList.SelectedItems[0].SubItems[1].Text +"\n\n" + _xmlTunnel.InnerText);
        _xmlTunnel.RemoveAll();

        //remove all empties
        XmlNodeList emptyElements = _xml.SelectNodes(@"//*[not(node())]");
        for (int i = emptyElements.Count -1; i >= 0; i--) {
        emptyElements[ i ].ParentNode.RemoveChild(emptyElements[ i ]); }

此代码工作正常,直到我有两个具有相同本地值的端口。它总是选择它到达的第一个元素(即Local=19 和Remote=21,即使您尝试选择Local=19 和Remote=22 的节点)。我尝试将 xpath 表达式切换为“and”而不是“|”在 SelectSingleNode 方法中,但会出现错误,并显示“表达式必须计算为节点集”。这让我认为当我使用“and”时我正在评估布尔值。

通过循环选择第一个元素并循环直到第二个元素匹配是更好的方法吗?正如我之前所说,我对 C# 中的 xpath/xml 表达式没有太多经验,也许有更好的方法。如果它对我使用 Windows 窗体和 .net 4.0 有帮助,在此窗体中,端口值将填充详细视图中的两列列表视图。

I am new to xpath in C# and I am trying to select an element that has two specific values. This is what the XML format looks like

<?xml version="1.0" encoding="utf-8"?>
<Manager>
  <SSH>
    <Tunnels>
      <Port>
        <Local>443</Local>
        <Remote>443</Remote>
      </Port>
      <Port>
        <Local>5432</Local>
        <Remote>5432</Remote>
      </Port>
      <Port>
        <Local>19</Local>
        <Remote>21</Remote>
      </Port>
      <Port>
        <Local>19</Local>
        <Remote>22</Remote>
      </Port>
    </Tunnels>
  </SSH>
</Manager>

I was trying to select a 'Port' that had the values from a previous form so i can delete that specific entry from the xml. This was the code i was using:

        //remove children from selected
        XmlNode _xmlTunnel = _xml.SelectSingleNode("/Manager/SSH/Tunnels/Port[Local=" + _local + "] | /Manager/SSH/Tunnels/Port[Remote=" + _remote + "]");
        MessageBox.Show("Local " + sshList.SelectedItems[0].Text + " Remote " + sshList.SelectedItems[0].SubItems[1].Text +"\n\n" + _xmlTunnel.InnerText);
        _xmlTunnel.RemoveAll();

        //remove all empties
        XmlNodeList emptyElements = _xml.SelectNodes(@"//*[not(node())]");
        for (int i = emptyElements.Count -1; i >= 0; i--) {
        emptyElements[ i ].ParentNode.RemoveChild(emptyElements[ i ]); }

This code works fine until I have two Ports with the same Local Value. It always selects the first element that it comes to (i.e. Local=19 and Remote=21, even if you try to select the node where Local=19 and Remote=22). I tried switching the xpath expression to 'and' instead of '|' in the SelectSingleNode method but that errors out with a "Expression must evaluate to a node-set". Which makes me think that I am evaluating out to a boolean when I use 'and'.

Is the better way to do this by a loop where a select the first element and loop until the second one matches? As I said before i do not have much experience with xpath/xml expressions in C#, perhaps there is a better way. If it helps I am using windows forms and .net 4.0, in this form the port values fill a two column list view in detail view.

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

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

发布评论

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

评论(1

゛时过境迁 2024-11-12 22:13:03

您需要在端口节点上“与”2 个条件,例如:

"/Manager/SSH/Tunnels/Port[Local=" + _local + " and  Remote=" + _remote + "]"

在您的情况下,您正在对 2 个集合(其中 Local=19 和另一个集合(Remote=21))进行并集。

You need to "and" 2 conditions on Port node like:

"/Manager/SSH/Tunnels/Port[Local=" + _local + " and  Remote=" + _remote + "]"

In your case you are doing union of 2 sets where Local=19 and another where Remote=21.

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