2010-06-07 4 views
0

내 XML 파일은 다음과 같습니다. 스키마와 일반 요소가 섞여 있습니다.C#에서 XPathNavigator 사용에 대한 도움말이 필요하십니까?

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

당신이 나에게 어떤 제안을 전해 주 시겠어요 (스키마의 모든 콘텐츠를 통과하여) 요소가에서 시작 루프합니다. 고맙습니다.

답변

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()); 
1
 XPathNavigator navigator = doc.CreateNavigator(); 
     if (navigator == null) 
     { 
      return; 
     } 
     foreach (XPathNavigator nav in 
      navigator.Select("/" + "EquipmentConstants" + "/" + "EquipmentConstant")) 
     { 

     } 
+0

@Arseny, Hello, 내 XML 파일에는 하나의'EquipmentConstants' 만 있습니다. 코드를 기반으로 foreach를 사용하지 않아도됩니까? –

+0

당신은 루트 요소 EquipmentConstants를 가지고 있다고 가정합니다. Where는 EquipmentConstant의 자식입니다. 그것이 맞다면 위의 코드는 자식 노드를 통해 탐색합니다. – Arseny

+0

'navigator.MoveToRoot()'다음에 코드를 삽입했습니다. 코드가 작동하지 않는다는 것을 알았습니다. select()가 작동하지 않는 것 같습니다. 'foreach' 루프는'EquipmentConstants' 블록으로 들어 가지 않습니다. –

관련 문제