简体   繁体   中英

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.

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

Since StringReader extends TextReader, this should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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