有没有一种方法来创建一个字符串一个SyndicationFeed? [英] Is there a way to create a SyndicationFeed from a String?

查看:133
本文介绍了有没有一种方法来创建一个字符串一个SyndicationFeed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重新创建XML数据的SyndicationFeed对象(System.ServiceModel.Syndication)已被存储在本地。

I'm trying to recreate a SyndicationFeed object (System.ServiceModel.Syndication) from XML data that has been stored locally.

如果我是用的XMLDocument工作,这将是easy.I'd调用的loadXML(串)。

If I were working with XMLDocument, this would be easy.I'd call LoadXml(string).

该SyndicationFeed将只能从一个XMLReader加载。 XMLReader可以只需要一个流或其他的XMLReader或TextReader的。

The SyndicationFeed will only load from an XMLReader. The XMLReader will only take a Stream or another XMLReader or a TextReader.

由于为XMLDocument将加载一个字符串,我试图做到这一点,如下所示(以扩展方法的形式):

Since XMLDocument will load a string, I've tried to do this as follows (in the form of a Extension Method):

    public static SyndicationFeed ToSyndicationFeed(this XmlDocument document)
    {
        Stream thestream = Stream.Null;
        XmlWriter thewriter = XmlWriter.Create(thestream);

        document.WriteTo(thewriter);

        thewriter.Flush();
        XmlReader thereader = XmlReader.Create(thestream);

        SyndicationFeed thefeed = SyndicationFeed.Load(thereader);

        return thefeed;
    }

我不能得到这个工作。流总是空的,即使XmlDocument的填充了饲料被装入SyndicationFeed。

I can't get this to work. The Stream is always empty even though the XMLDocument is populated with the Feed to be loaded into the SyndicationFeed.

任何帮助或指针,你可以给将是最有帮助的。

Any help or pointers you can give would be most helpful.

谢谢, 罗伯托

推荐答案

由于StringReader扩展TextReader的,这应该工作:

Since StringReader extends TextReader, this should work:

TextReader tr = new StringReader(xmlString);
XmlReader xmlReader = XmlReader.Create(tr);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

这篇关于有没有一种方法来创建一个字符串一个SyndicationFeed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆