0

在我的 WPF 应用程序中,我想编写代码来读取 Atom xaml Feed。Web links就像我www.myweblink/NewXaml.xaml希望我的代码使用此链接并从此链接中定义的此 xaml 文件中获取标题和描述。

ATOM XAML.文件

    <?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 文件中无效。

Q1:如何从这个 xaml 文件中获取标题和描述?

Q2:我希望我的程序自动转到此链接,而不是我必须将此文件保存在我的 PC 上并提供此代码的路径以使其硬编码。

谁能帮帮我吗。

4

2 回答 2

2

@Gusdor 是对的,我也认为您在谈论Atom XML FeedXML Feed 这是与Article1Article2相关的详细文章

请阅读这些文章。这些对于理解RSS和的基础知识非常有帮助SyndicationFeed

这里的代码是:

 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  
 }
于 2013-10-25T13:16:25.367 回答
1

您将需要使用 XML 解析器。就像XDocumentSystem.Xml.Linq命名空间中一样

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。

于 2013-10-25T11:49:55.950 回答