【问题标题】:Need Help About Using XPathNavigator in C#?需要有关在 C# 中使用 XPathNavigator 的帮助吗?
【发布时间】:2010-06-07 17:46:35
【问题描述】:

我的 XML 文件如下。它混合了模式和普通元素。

<?xml version="1.0" encoding="utf-8"?>
<!-- R1 -->
<ax:root xmlns:ax="http://amecn/software/realtime/ax">
  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="EquipmentConstants">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element minOccurs="0" maxOccurs="unbounded" ref="EquipmentConstant" />
        </xsd:sequence>
      </xsd:complexType>
      <xsd:unique name="id">
        <xsd:selector xpath=".//EquipmentConstant" />
        <xsd:field xpath="@id" />
      </xsd:unique>
    </xsd:element>
    ......
    ......
  </xsd:schema>
  <EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <EquipmentConstant id="0">
      <Name>SerialNumber</Name>
      <Group>SYSTEM</Group>
      <Data>
        <Value min="0" max="10000000" scale_factor="0" unit="U_NO_UNITS" permission="NolimitedAndNoChangeable" type="xsd_string" enum="" flag="0">0</Value>
      </Data>
      <Description>Serial Number</Description>
    </EquipmentConstant>
    .....
    .....
  </EquipmentConstants>
</ax:root>

我的 C# 代码如下。我想循环元素从(通过传递架构的所有内容)开始

&lt;EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;

XPathDocument doc = new XPathDocument("test.xml");
                XPathNavigator navigator = doc.CreateNavigator();

                navigator.MoveToRoot(); // <?xml version="1.0" encoding="utf-8"?>
                //navigator.MoveToFirstChild();   // <!-- R1 -->
                // 1st, I tried to use MoveToChield(), But I failed to move there.
                navigator.MoveToChild("EquipmentConstants"); 
                // Then, I also tried to use SelectSingleNode(). But I failed too. 
                navigator.SelectSingleNode("ax/EquipmentConstants");
                while (navigator.MoveToNext())
                {
                // do something.
                } 

你能给我一些建议吗?谢谢。

【问题讨论】:

    标签: c# xml xpath xpathnavigator


    【解决方案1】:
            XPathNavigator navigator = doc.CreateNavigator();
            if (navigator == null)
            {
                return;
            }
            foreach (XPathNavigator nav in
                navigator.Select("/" + "EquipmentConstants" + "/" + "EquipmentConstant"))
            {
    
            }
    

    【讨论】:

    • @Arseny,您好,我的 xml 文件中只有一个 EquipmentConstants。根据您的代码,我可以不使用 foreach 吗?
    • 我假设您有根元素 EquipmentConstants,其中是子 EquipmentConstant 的列表。如果正确,我上面的代码将通过子节点导航。
    • navigator.MoveToRoot() 之后,我插入了您的代码。我发现代码不起作用。似乎 select() 无法正常工作。 foreach 循环不会进入EquipmentConstants 的块。
    • 好的。在 Select 方法中尝试这个“/ax:root/EquipmentConstantsEquipmentConstant”。
    • 您在 select 方法中插入的查询是一个 XPath 表达式。这是 Xpath 语法w3schools.com/XPath/xpath_syntax.asp
    【解决方案2】:

    我的解决方案如下。

                XPathDocument doc = new XPathDocument("test.xml");
                XPathNavigator navigator = doc.CreateNavigator();
    
                navigator.MoveToRoot(); // <?xml version="1.0" encoding="utf-8"?>
                navigator.MoveToFirstChild();   // <!-- R1 -->
                navigator.MoveToNext(); //   <ax:root xmlns:ax="http://amecn/software/realtime/ax">
                navigator.MoveToChild("EquipmentConstants", ""); // <EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
                navigator.MoveToFirstChild();   // <EquipmentConstant id="0">
                do
                {
                // Loop body;
                } while (navigator.MoveToNext());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2011-09-16
      • 2011-09-15
      • 2014-08-31
      • 2015-06-23
      相关资源
      最近更新 更多