Is there a way to create a SyndicationFeed from a String?

只谈情不闲聊 提交于 2019-12-12 19:18:57

问题


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

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

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

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

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.

Thanks, Roberto


回答1:


Since StringReader extends TextReader, this should work:

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


来源:https://stackoverflow.com/questions/2686533/is-there-a-way-to-create-a-syndicationfeed-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!