views:

201

answers:

2

Hello.

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"&gt;
  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    <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"&gt;
    <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>

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

Could you please give me some suggestion. Thank you.

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

        }
Arseny
@Arseny, Hello, There is only one `EquipmentConstants` in my xml file. Can I simply don't use foreach based on your code?
Nano HE
I assume you got root element EquipmentConstants where is a list of children EquipmentConstant. if it is right my code above navigates via children nodes.
Arseny
after `navigator.MoveToRoot()`, I inserted your code. I found the code doesn't work. Seems like select() can't work. the `foreach` loop won't enter to the block of `EquipmentConstants`.
Nano HE
ok. try this one "/ax:root/EquipmentConstantsEquipmentConstant" in Select method.
Arseny
After updated Select(). Run time exception throw out.`An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll``Additional information: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.`
Nano HE
the query you are inserting in the select method is a XPath expression. here is a Xpath syntax http://www.w3schools.com/XPath/xpath_syntax.asp
Arseny
@Arseny, Thank you.
Nano HE
A: 

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"&gt;
            navigator.MoveToChild("EquipmentConstants", ""); // <EquipmentConstants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;

            navigator.MoveToFirstChild();   // <EquipmentConstant id="0">
            do
            {
            // Loop body;
            } while (navigator.MoveToNext());
Nano HE