2013-10-25 40 views
0

在我的WPF應用程序中,我想編寫代碼來讀取Atom xaml Feed。有Web linkswww.myweblink/NewXaml.xaml我希望我的代碼使用此鏈接,並從此鏈接中定義的此xaml文件中獲取標題和說明。如何讀取WPF中的Atom XAML文件?

ATOM XAML.FILE

<?xml version='1.0' encoding='UTF-8' ?> 
    <rss version='2.0' xmlns:atom='http://www.w7.org7/Atom'> 
    <channelWay> 
    <title>Text1 - Network</title> 
    <atom:link href='http://link1/myxaml.xml' rel='self' type='typeOne/rss+xml' /> 
    <link>http://website.com</link> 
    <description> This is New Description </description> 
    <lastBuildDate>Fri, 2 Oct 2011 00:00:00 +0500</lastBuildDate> 
    <language>en-us</language> 
    <item> 
    <title> 
     My First Title 
    </title> 
    <link>http://website.com/PageOne.aspx?ID=123790</link> 
    <guid>http://website.com/PageTwo.aspx?ID=123790</guid> 
    <description> 
     My First Description 
    </description> 
    <pubDate>Fri, 2 Oct 2011 13:10:00 +0500</pubDate> 
    </item> 
    <item> 
    <title> 
     My Second Title 
    </title> 
    <link> 
<link>http://website.com/PageOne1.aspx?ID=123790</link> 
    <guid>http://website.com/PageTwo1.aspx?ID=123790</guid> 
    <description> 
     My Second Description 
    </description> 
    <pubDate>Fri, 2 Oct 2011 13:10:00 +0500</pubDate> 
    </item> . . . . . . 

代碼改爲XAML文件:

public Window1() 
     { 
      InitializeComponent(); 

      FlowDocument content = null; 

      OpenFileDialog openFile = new OpenFileDialog(); 
      openFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*"; 

      if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       FileStream xamlFile = openFile.OpenFile() as FileStream; 
       if (xamlFile == null) return; 
       else 
       { 
        try 
        { 
         content = XamlReader.Load(xamlFile) as FlowDocument; 
         if (content == null) 
          throw(new XamlParseException("The specified file could not be loaded as a FlowDocument.")); 
        } 
        catch (XamlParseException e) 
        { 
         String error = "There was a problem parsing the specified file:\n\n"; 
         error += openFile.FileName; 
         error += "\n\nException details:\n\n"; 
         error += e.Message; 
         System.Windows.MessageBox.Show(error); 
         return; 
        } 
        catch (Exception e) 
        { 
         String error = "There was a problem loading the specified file:\n\n"; 
         error += openFile.FileName; 
         error += "\n\nException details:\n\n"; 
         error += e.Message; 
         System.Windows.MessageBox.Show(error); 
         return; 
        } 

        FlowDocRdr.Document = content; 
       } 
     } 

錯誤:它產生的錯誤,那rss在XAML filde是無效的。

問題1:如何從這個xaml文件獲得標題和描述?

問題2:我希望我的程序自動轉到此鏈接,而不是這個,我必須將此文件保存在我的PC上,併爲此代碼添加路徑以使其硬編碼。

誰能請幫助我。

+2

你_sure_,這是一個有效的流程文檔_XAML_文件?看起來像一個Atom _XML_飼料給我。 – Gusdor

+0

@Gusdor哦,是的。我很抱歉,我忘記提到這個:( - – user2835256

+0

你應該修改你對XAML的引用。這裏沒有xaml文件。 – Gusdor

回答

2

@Gusdor是對的,我想你也是在談論Atom XML Feed。 下面是詳細的文章涉及到XML Feed Article1Article2

請仔細閱讀這些文章。這些對於瞭解RSSSyndicationFeed的基本知識非常有幫助。

下面的代碼是:

XmlReader reader = XmlReader.Create(@"http://link1/myxaml.xml"); 
SyndicationFeed feed = SyndicationFeed.Load(reader); 
var titles = from item in feed.Items 
      select new 
       { 
       item.Title.Text, 
       }; 
var descriptions = from item in feed.Items 
     select new 
      { 
      item.Summary.Text 

      }; 

foreach (var t in titles) 
{ 
title_textbox.Text += t.Text + " ";    //Your All Titles Here 

} 
foreach (var des in descriptions) 
{ 
description_textbox.Text += des.Text + " ";  //Your All Descriptions Here 
} 
+0

謝謝你的好友:)我需要這個 – user2835256

1

您將需要使用XML解析器。就像在System.Xml.Linq命名空間XDocument

FileStream xamlFile = openFile.OpenFile() as FileStream; 
XDocument xdoc = XDocument.Load(xamlFile); 

//get title 
string title = xdoc.Element("Title").Value; 

//get description of first link 
string firstLinkDesc = xdoc.Element("Link").Element("Description").Value; 

注 - 我相信你是混淆XAML的關係和XML - 它們是不同的格式。 XAML基於XML。 Atom是用於交付基於XML的訂閱源的格式。

+0

謝謝,但如何添加鏈接:( – user2835256

+2

@ user2835256有足夠的空間來擴大你的視野,讓大腦和工作爲你自己一點點:) – Gusdor