2014-05-20 2 views
1

XML 데이터 파일에 특정 레이아웃이 있어야하는 공급 업체가 있습니다. 구조는 XSD 파일에 정의되어 있으며 매월 구조가 업데이트 (요소가 재배치되거나 제거 될 수 있음) 될 수 있습니다 (새 XSD 파일이 발송 됨).특정 XML 요소 읽기

그러나 XML 파일이 있지만 XSD 요구 사항과 동일한 순서로 요소를 배치해야합니다. 아래는 XML 파일을 읽을 수있는 코드입니다. 내 post here을 기반으로 XSD 파일을 반복 할 수 있습니다.

내 문제는 XML 파일에서 특정 요소를 반환 할 수 없다는 것입니다. 어떻게해야합니까?

 foreach (var XSDsection in sections) 
     { 
      //Get Section Element; 
      string SchemaSchedule = XSDsection.Attribute("name").Value; 
      var SchemaSectionSchedule = XSDsection.Element(prefix + "complexType") 
           .Element(prefix + "sequence") 
           .Elements(prefix + "element"); 
      foreach (var schemaSection in SchemaSectionSchedule) 
      { 
       //Get child element; 
       string schemaElement = schemaSection.Attribute("name").Value; 
       var XMLsectionSchedule = xDoc.Descendants(SchemaSchedule); 

       foreach (XElement element in xDoc.Element(SchemaSchedule).Descendants()) 
       { 
        string value = element.Value;  //returns value of next element; 
        string el = element.ToString();  //returns the next element and value from xml file; 
       } 
      } 
     } 

답변

0

나는 XML에서 최고의 아니에요,하지만 난 당신이 뭔가를 할 수 있습니다 생각이 만 일치하는 첫 번째 요소를 반환 할 것

foreach (var XSDsection in sections) 
    { 
     //Get Section Element; 
     string SchemaSchedule = XSDsection.Attribute("name").Value; 
     var SchemaSectionSchedule = XSDsection.Element(prefix + "complexType") 
          .Element(prefix + "sequence") 
          .Elements(prefix + "element"); 
     foreach (var schemaSection in SchemaSectionSchedule) 
     { 
      //Get child element; 
      string schemaElement = schemaSection.Attribute("name").Value; 
      var XMLsectionSchedule = xDoc.Descendants(SchemaSchedule); 

      foreach (XElement element in xDoc.Element(SchemaSchedule).Descendants()) 
      { 
       string value = element.Value;  //returns value of next element; 
       string el = element.ToString();  //returns the next element and value from xml file; 
       if(value == myDesiredValue) 
       { 
        return element; // if you want the element, return it 
       } 
      } 
     } 
    } 

참고. 일단 발견되면 요소를 반환하고 루핑을 중단합니다.

+0

감사합니다. 나는 그것을 시도 할 것이다. 각 요소는 데이터 파일에서 한 번만 발생합니다. –

+0

'return schemaSection;이나'return XSDSection; '과 같은 부모 루프의 다른 요소를 반환 할 수도 있습니다. – MightyLampshade