2013-06-17 2 views
0

나는이 strucure와 XML이 있습니다XDocument가있는 노드에서 같은 이름의 요소를 여러 개 가져 오는 방법은 무엇입니까?

<news> 
     <id><![CDATA[1]]></id> 
     <title><![CDATA[My title]]></title> 
     <date><![CDATA[17-06-2013]]></date> 
     <machine><![CDATA[a]]></machine> 
     <machine><![CDATA[b]]></machine> 
     <machine><![CDATA[c]]></machine> 
     <machine><![CDATA[d]]></machine> 
    </news> 
    <news> 
     <id><![CDATA[2]]></id> 
     <title><![CDATA[My title 2]]></title> 
     <date><![CDATA[17-06-2013]]></date> 
     <machine><![CDATA[a]]></machine> 
     <machine><![CDATA[b]]></machine> 
     <machine><![CDATA[c]]></machine> 
     <machine><![CDATA[d]]></machine> 
    </news> 

을 내가 같이 읽어 :

var datas = from query in loadedData.Descendants("news") 
          select new News 
          { 
           Title = (string)query.Element("title"), 
           Id = (string)query.Element("id"), 
           StrDate = (string)query.Element("date"), 
           list = query.Elements("machine") 
          }; 

코드

list = query.Elements("machine") 

이 작동하지 않습니다. 태그 "기계"가있는 요소 목록을 얻는 방법

+1

엘리먼트 –

답변

1

아래에 언급 된 코드가 제대로 작동합니다. 목록의 대상으로 고려한 목록

var datas = from query in loadedData.Descendants("news") 
           select new News 
           { 
            Title = (string)query.Element("title"), 
            Id = (string)query.Element("id"), 
            StrDate = (string)query.Element("date"), 
            list = (from xele in query.Descendants("machine") 
              select xele.Value).ToList<string>(); 
           }; 
+0

대신 자손을 사용해보세요. 감사합니다! – Anthony

관련 문제