有没有一种方法来创建一个字符串一个SyndicationFeed?字符串、方法来、创建一个、SyndicationFeed

2023-09-06 08:42:17 作者:晚安安入我心 *

我试图重新创建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);