2012-05-10 2 views
0

가능한 중복 : 나는 C# 윈도우 응용 프로그램에서 Linq에를 사용하여 XML 파일을 읽기 위해 노력하고
Use LINQ to read all nodes from XML모든 노드

. xml 문자열의 샘플은 아래에 나와 있습니다.

<Root> 
<Name>John Doe</Name> 
<Data>FBCCF14D504B7B2DBCB5A5BDA75BD93B</Data> 
<customer>true</customer> 
<Accounts>1</Accounts> 
<dataSet> 
    <Type1>Found matching records.</Type1> 
    <Type2>No matches found.</Type2> 
    <Type3>Found matching records.</Type3> 
</dataSet> 
</Root> 

나는 내가뿐만 아니라 <customer> 태그를 읽고 싶은 <dataset> 태그와 <datatag> 내부의 모든 데이터를 표시합니다.

회원 (문자열 유형, 문자열 상태)이있는 클래스를 만들었습니다. 어디 형식 type1, 2 ... 및 상태 저장하려면 원하는 형식 노드 안에 저장하려면 원하는.

나는이 작업을 수행 할 수 있어요하지만 코드에서 난 내가 모든 유형을 언급 해달라고하는 일반적인 코드를 갖고 싶어

type1 = (string)row.Element("type1"), type2=(string)row.Element("type2"), 주고있다. 즉, 태그 이름을 언급하지 않고 태그의 모든 자식 노드를 읽고 싶습니다. 나는 이것을 구글에서 검색하는데 2 시간을 보냈지 만 아직 아무것도 찾지 못했다.

예상 출력

정보를 클래스 개체 (유형 및 상태)에 저장하십시오.

와 나는 내가 사람이 이미 어떤 도움이 아주 많이 이해할 수있을 것이다

고객인지 여부를 알 수 있도록 고객 태그를 읽고 싶어.

감사

업데이트

입력에 따르면 라파엘 Althaus로부터받은

나는 다음과 같은 코드가 있습니다

var list = xml.Descendants("dataSet").Elements() 
      .Select(m => new CustomerInfo 
          { 
           Type = m.Name.LocalName, 
           Value = m.Value 
          }).ToList(); 


     foreach (CustomerInfo item in list) 
     { 
      MessageBox.Show(item.Type+ " "+item.Value); 

     } 

그리고 난 더 쓴 고객 태그를 읽는을 암호.

var isCustomer = from customer in xmlDoc.Descendants("Root") 
      select new 
      { 
       customer = tutorial.Element("customer").Value, 
      } 

둘 다 한 쿼리에서 수행 할 수 있습니까? 또는이 방법은 성능에 그리 무겁지 않으므로 이것을 사용할 수 있습니까?

+0

나는 여기 http://stackoverflow.com/questions/5978926/c-using-linq-to-get-a-ienumerable-collection-of-xml-nodes-to-traverse – ace120387

답변

1

그런 식 으로요?

var q = xml.Descendants("dataSet").Elements() 
      .Select(m => new 
          { 
           type = m.Name.LocalName, 
           value = m.Value 
          }).ToList(); 

당신은 직접 "회원들과 클래스"당신의 목록을 채울 수 있습니다

var list = xml.Descendants("dataSet").Elements() 
       .Select(m => new <TheNameOfYourClass> 
           { 
            Type = m.Name.LocalName, 
            Value = m.Value 
           }).ToList(); 

편집 :

, 나는 다른 쿼리

을 할 것 "고객"값을 얻을 수
var customerElement = xml.Element("customer"); 
var isCustomer = customerElement != null && customerElement.Value == "true"; 

작은 기능으로 모든 것을 혼합 할 수 있습니다.

public IList<YourClass> ParseCustomers(string xmlPath, out isCustomer) { 
    var xml = XElement.Load(xmlPath); 
    var customerElement = xml.Element("customer"); 
    isCustomer = customerElement != null && customerElement.Value == "true"; 
    return xml.Descendants("dataSet").Elements() 
        .Select(m => new <YourClass> 
            { 
             Type = m.Name.LocalName, 
             Value = m.Value 
            }).ToList(); 
} 
+0

덕분에 부분적인 해답을 발견 작동하지만 데이터 집합 아래의 요소 만 반환합니다. 나는 태그도 원한다. 한 번에 그렇게 할 수있는 방법이 있습니까? xml.Descendants ("data") xml.Descendants ("root")> 에 의해 xml.Descendants ("dataSet")을 바꿀 때태그 아래에있는 모든 값 = 데이터 세트 및 값 = – ace120387

+0

: 목록을 제공 할 수 있습니까? 귀하의 XML 예제를 기반으로 예상 결과? –

+0

예상 결과 : 이미 완료된 클래스의 유형 및 값. 태그도 읽고 싶습니다. 그 사람이 이미 고객인지 여부를 알 수 있습니다. – ace120387

관련 문제