2012-02-22 3 views
1

RSS 피드를 http://b.fanfiction.net/atom/l/224/3/0/1/1/50/0/0/0/0에서 구문 분석하려고하지만 어떤 이유로 요소 이름으로 검색 할 수 없습니다. 나는 this page에서 지침을 따르고 있지만 그의 예는 위의 fanfiction 사이트에서 작동하지 않는 것 같습니다. 난 그냥 모든 요소 및 문서의 하위 요소를 나열 시도RSS 피드를 구문 분석 할 수 없음

private void getData_Click(object sender, RoutedEventArgs e) 
{ 
    String rss = "http://b.fanfiction.net/atom/l/224/3/0/1/1/50/0/0/0/0"; 

    HttpWebRequest request = HttpWebRequest.CreateHttp(rss); 
    request.BeginGetResponse(
     asyncCallback => 
     { 
      string data = null; 

      using (WebResponse response = request.EndGetResponse(asyncCallback)) 
      { 
       using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
       { 
        data = reader.ReadToEnd(); 
       } 
      } 
      Deployment.Current.Dispatcher.BeginInvoke(() => HttpsCompleted(data)); 
     } 
     , null); 
} 

private void HttpsCompleted(string feedString) 
{ 
    // build XML DOM from feed string 
    XDocument doc = XDocument.Parse(feedString); 

    // show title of feed in TextBlock 
    XElement feed = doc.Element("feed"); 
    XElement title = feed.Element("title"); 
    String txt_title = title.Value; 
    txtBlk_FeedDescription.Text = txt_title; 
    // add each feed item to a ListBox 
    foreach (var item in doc.Descendants("entry")) 
    { 
     listBox1.Items.Add(item.Element("title").Value); 
    } 
} 

실행하여 : 여기

내가 사용하고 코드의

foreach (XElement element in doc.Elements()) 
     { 
      txtBlk_FeedDescription.Text += element.Name.ToString() + "\n"; 
      foreach (XElement subelement in element.Elements()) 
      { 
       txtBlk_FeedDescription.Text += subelement.Name.ToString() + "\n"; 
      } 
     } 

그리고 그 다음 목록에 결과 :

{http://www.w3.org/2005/Atom은}

{,582,385,393,632 공급 10} 저작

{http://www.w3.org/2005/Atom} 표제

{http://www.w3.org/2005/Atom} 자막

{http://www.w3.org/2005/Atom} 링크

{http://www.w3.org/2005/Atom} 업데이트

{http://www.w3.org/2005/Atom} ID

{http://www.w3.org/2005/Atom} 입력

01 23,516,

{http://www.w3.org/2005/Atom} 항목

{http://www.w3.org/2005/Atom} 항목

...

내가 여기 어떻게해야 해요 무엇에 어떤 도움?

감사합니다.

답변

3

Linq to XML 쿼리는 이름으로 요소를 찾고 있지만 네임 스페이스를 포함하지는 않습니다. 원본 문서를 검사하는 경우는 TEH 다음 네임 스페이스를 가지고 있다고 볼 수 있습니다 :

xmlns="http://www.w3.org/2005/Atom" 

working with XML namespaces의 MSDN 문서를 참조하십시오. 예를 들어, 다음은 feed 요소를 선택합니다

XNamespace ns = "http://www.w3.org/2005/Atom"; 

XElement feed = doc.Element(ns + "feed"); 

당신은 모든 당신의 요소 이름을 네임 스페이스를 추가해야합니다.

관련 문제