views:

22

answers:

1

Hi, 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  A: 

Since StringReader extends TextReader, this should work:

TextReader tr = new StringReader(xmlString);
XmlReader xmlReader = XmlReader.Create(tr);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
Lee
*head desk* I forgot about StringReader. Let me try it.
Roberto Bonini
It worked. Thanks loads.
Roberto Bonini
No problem, glad it helped.
Lee