开发者

Need Help About Using XPathNavigator in C#?

开发者 https://www.devze.com 2023-01-02 18:07 出处:网络
My XML file as below. It mixed schema and normal elements. <?xml version=\"1.0\" encoding=\"utf-8\"?>

My XML file as below. It mixed schema and normal elements.

<?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>
  开发者_如何学C    <Description>Serial Number</Description>
    </EquipmentConstant>
    .....
    .....
  </EquipmentConstants>
</ax:root>

My C# code as below. I want to loop the elements start from (by pass all the content of schema)

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

Could you please give me some suggestion. Thank you.


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

        }


My solution as below.

            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());
0

精彩评论

暂无评论...
验证码 换一张
取 消