首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XPathNavigator和XPathNodeIterator遍历XML

使用XPathNavigator和XPathNodeIterator遍历XML
EN

Stack Overflow用户
提问于 2015-03-13 23:40:48
回答 1查看 531关注 0票数 1

我正在做一个项目,在这个项目中,我接受一个XML文件,并且需要遍历XML并输出特定节点的文本值,而我正在努力寻找实现这一点的最佳方法。

我正在使用下面的代码,它适用于我定义的单个XPath节点,它工作得很好,但我需要提取额外的节点及其相关文本。任何指向正确方向的指针都将不胜感激。我已经阅读了微软的XPathNavigatorXPathNodeIteratorXMLReader文档,并使用了他们的例子,但似乎没有一个适合这个特定的用例。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            XPathDocument document = new XPathDocument("Security_Policy.xml");
            XPathNavigator navigator = document.CreateNavigator();
            XPathNodeIterator nodes = navigator.Select("//Rule_Number");
            while (nodes.MoveNext())
            {
                Console.WriteLine(nodes.Current.Value);
            }
        }
    }
}

下面是我正在使用的相关XML块。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
      <rule>
        <Name></Name>
        <Class_Name>security_rule</Class_Name>
        <Rule_UUID>{9BA1463A-00D0-4819-88B6-650407733A96}</Rule_UUID>
        <Rule_Number>1</Rule_Number>
        <action>
          <action>
            <Name>accept</Name>
            <Class_Name>accept_action</Class_Name>
            <action><![CDATA[]]></action>
            <identity_settings>
              <Name></Name>
              <Class_Name>identity_action_settings</Class_Name>
              <allow_ad_query>true</allow_ad_query>
              <allow_captive_portal>true</allow_captive_portal>
              <allow_identity_agent>true</allow_identity_agent>
              <allowed_sources><![CDATA[All Sources]]></allowed_sources>
              <redirect_to_captive_portal>false</redirect_to_captive_portal>
              <require_packet_tagging>false</require_packet_tagging>
              <type><![CDATA[identity_action_settings]]></type>
            </identity_settings>
            <macro><![CDATA[RECORD_CONN]]></macro>
            <type><![CDATA[accept]]></type>
          </action>
        </action>
        <comments><![CDATA[]]></comments>
        <disabled>false</disabled>
        <dst>
          <Name></Name>
          <Class_Name>rule_destination</Class_Name>
          <members>
            <reference>
              <Name></Name>
              <Table>network_objects</Table>
            </reference>
            <reference>
              <Name></Name>
              <Table>network_objects</Table>
            </reference>
          </members>
          <compound></compound>
          <op><![CDATA[]]></op>
        </dst>
        <global_location><![CDATA[middle]]></global_location>
        <install>
          <Name></Name>
          <Class_Name>rule_install</Class_Name>
          <members>
            <reference>
              <Name>Any</Name>
              <Table>globals</Table>
            </reference>
          </members>
          <compound></compound>
        </install>
        <name><![CDATA[]]></name>
        <services>
          <Name></Name>
          <Class_Name>rule_services</Class_Name>
          <members>
            <reference>
              <Name>Any</Name>
              <Table>globals</Table>
            </reference>
          </members>
          <compound></compound>
          <op><![CDATA[]]></op>
        </services>
        <src>
          <Name></Name>
          <Class_Name>rule_source</Class_Name>
          <members>
            <reference>
              <Name></Name>
              <Table>network_objects</Table>
            </reference>
            <reference>
              <Name></Name>
              <Table>network_objects</Table>
            </reference>
          </members>
          <compound></compound>
          <op><![CDATA[]]></op>
        </src>
        <through>
          <Name></Name>
          <Class_Name>rule_vpn</Class_Name>
          <members>
            <reference>
              <Name>Any</Name>
              <Table>globals</Table>
            </reference>
          </members>
          <compound></compound>
        </through>
        <time>
          <time>
            <Name>Any</Name>
            <Table>globals</Table>
          </time>
        </time>
        <track>
          <track>
            <Name>Log</Name>
            <Table>tracks</Table>
          </track>
        </track>
      </rule>
EN

回答 1

Stack Overflow用户

发布于 2015-03-13 23:58:58

我认为您的XPath路径是不正确的,因为"Rule_Number“元素不是文档的根元素,但是我不是XPath专家,所以请对以下XPath建议持保留态度:

代码语言:javascript
复制
//rule/Rule_Number

下面是另一种方法,因为我极力主张尽可能使用linq- to -xml,因为它使用起来非常直观:

代码语言:javascript
复制
XDocument doc = XDocument.Load("Security_Policy.xml");
var ruleNodes = doc.Root.Elements("Rule_Number");
foreach(var node in ruleNodes)
{
    Console.WriteLine(node.Value);
}

编辑:

如果您想要元素而不管它在XML文档的层次结构中处于什么位置,那么可以使用doc.Descendants("TAG NAME")来获取具有匹配的“标记名”的任何内容(从技术上讲,数据是该字段中的一个XName,但现在不必担心这一点)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29036417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档