【问题标题】:How to read an Atom XAML File in WPF?如何在 WPF 中读取 Atom XAML 文件?
【发布时间】:2013-10-25 11:29:56
【问题描述】:

在我的 WPF 应用程序中,我想编写代码来读取 Atom xaml Feed。有Web linkswww.myweblink/NewXaml.xaml 我希望我的代码使用这个链接并从这个链接中定义的这个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;
                }
        }

错误:它会生成错误,Xaml 文件中的rss 无效。

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

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

谁能帮帮我。

【问题讨论】:

  • 确定这是一个有效的流文档XAML文件吗?看起来像是给我的 Atom XML 提要。
  • @Gusdor 哦,是的。我很抱歉我忘了提这个:(--
  • 您可能应该修改对 XAML 的引用。这里没有 xaml 文件。
  • @Gusdor 我是 C# 新手。顶部的代码有我想阅读的 xaml 代码。你能帮我解决这个问题吗?

标签: c# .net wpf xaml


【解决方案1】:

@Gusdor 是对的,我也认为您在谈论 Atom XML Feed。 这里有与XML Feed相关的详细文章 Article1, Article2

请阅读这些文章。这些对于理解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  
 }

【讨论】:

  • 谢谢哥们:) 我需要这个
【解决方案2】:

您将需要使用 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 的提要格式。

【讨论】:

  • 谢谢,但是如何添加链接:(
  • @user2835256 这里有足够的东西来扩展你的视野,动动脑筋,为自己努力一点;)
猜你喜欢
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多