开发者

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

开发者 https://www.devze.com 2022-12-27 22:13 出处:网络
I\'m trying to recreate a SyndicationFeed object (System.ServiceModel.Syndication) from XML data that has been stored locally.

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):

    pub开发者_开发技巧lic 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


Since StringReader extends TextReader, this should work:

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

精彩评论

暂无评论...
验证码 换一张
取 消