2011-10-19 2 views
2

이전 질문 (http://stackoverflow.com/questions/7817619/unknown-error-using-digg-ap-and-ur-handler-silverlight)에서 계속하겠습니다. 당신은 응답을 위해) 나는 이제 더 적은 errory 질문을 따르도록한다.Digg API를 사용하여 XML을 LINQ로

this에서 데이터를 추출하려면 다음 XML을 LINQ 코드로 가져옵니다.

var stories = from story in document.Descendants("story") 
        //where story.Element("thumbnail") != null 
        select new DiggStory 
        { 
         Id = (string)story.Attribute("story_id"), 
         Title = (string)story.Element("title"), 
         Description = (string)story.Element("description"), 
         ThumbNail = (string)story.Element("thumbnail").Attribute("src"), 
         //HrefLink = (string)story.Attribute("permalink"), 
         NumDiggs = (int)story.Attribute("diggs") 
        }; 

목적으로하지만, 디그 API로 보는 것은 내가 the following XML file을 생성하는 새로운 API가 될 것을 선호 오래된 것처럼이 작동합니다.

이제이 새로운 XML 파일을 활용하기 위해 XML을 LINQ 코드로 조정하는 방법에 대해 궁금합니다. 나는 문제가

var stories = from story in document.Descendants("story") 

에 알고하지만이 new XML file 더 많은 수준을 가지고 있기 때문에 나는 그것을 변경해야 모르겠어요. 나는 라인에서 뭔가를 생각하고 있었다.

var stories = from item in document.Descendants("stories") 

그러나 그것은 그렇게 보이지 않는다.

이 문제와 다른 문제를 해결해 주신 데 대해 다시 한 번 감사드립니다. 이것은 진정으로 훌륭한 웹 사이트입니다!

답변

1

시작이와 토마스 감사 :

var stories = from item in document.RootElement.Element("stories").Descendants("item") 
       select new DiggStory 
       { 
        Id = item.Element("story_id").Value 
        // ...etc 
       } 

당신은 LINQ to XML으로 XML 문서를 구문 분석에 대해 자세히 알아볼 수 있습니다. 특히 당신이 사용하는 것은 XElement documentation 일 것입니다. 이것은 XML 문서의 각 "요소"(태그)에 대해 정의 된 모든 메소드와 속성을 보여줍니다.