0

私の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#コード。要素をループしたい(スキーマのすべてのコンテンツを渡す)

<EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

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

提案をお願いします。ありがとうございました。

4

2 に答える 2

1
        XPathNavigator navigator = doc.CreateNavigator();
        if (navigator == null)
        {
            return;
        }
        foreach (XPathNavigator nav in
            navigator.Select("/" + "EquipmentConstants" + "/" + "EquipmentConstant"))
        {

        }
于 2010-06-07T07:24:54.650 に答える
0

以下の私の解決策。

            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());
于 2010-06-07T09:46:35.137 に答える