简体   繁体   English

有没有一种方法可以从字符串创建SyndicationFeed?

[英]Is there a way to create a SyndicationFeed from a String?

I'm trying to recreate a SyndicationFeed object (System.ServiceModel.Syndication) from XML data that has been stored locally. 我正在尝试从已本地存储的XML数据重新创建SyndicationFeed对象(System.ServiceModel.Syndication)。

If I were working with XMLDocument, this would be easy.I'd call LoadXml(string). 如果我使用XMLDocument,这会很容易,我会调用LoadXml(string)。

The SyndicationFeed will only load from an XMLReader. SyndicationFeed将仅从XMLReader加载。 The XMLReader will only take a Stream or another XMLReader or a TextReader. XMLReader仅将使用Stream或另一个XMLReader或TextReader。

Since XMLDocument will load a string, I've tried to do this as follows (in the form of a Extension Method): 由于XMLDocument将加载字符串,因此我尝试按以下方式进行操作(以扩展方法的形式):

    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;
    }

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. 即使XMLDocument填充了要加载到SyndicationFeed中的Feed,Stream始终为空。

Any help or pointers you can give would be most helpful. 您可以提供的任何帮助或指示将最有帮助。

Thanks, Roberto 谢谢罗伯托

Since StringReader extends TextReader, this should work: 由于StringReader扩展了TextReader,因此应该可以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM