Chinmaya
Chinmaya

Reputation: 1

conversion of atom feed to rss feed

i want to convert a XML find having atom feed into rss feed. I used the following method :

public void AtomToRss20(string atomFileName, string rssFileName)
    {
        ConvertToRss20(ReadFeed(atomFileName), rssFileName);
    }

    private static void ConvertToRss20(SyndicationFeed feed, string rssFileName)
    {
        Rss20FeedFormatter rss2 = new Rss20FeedFormatter(feed);
        XmlWriter writer = XmlWriter.Create(rssFileName);
        rss2.WriteTo(writer);
        // You can also use 
        // feed.SaveAsRss20(writer);           
    }

    private static SyndicationFeed ReadFeed(string fileName)
    {
        StreamReader stream = new StreamReader(fileName);
        XmlReader reader = XmlReader.Create(stream);
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        return feed;        
    }

It executes successfully but when i viewed the file I found that it is incomplete and terminated with missing closing tags. Can somebody please tell the problem in this or some other method for the conversion.

Upvotes: 1

Views: 663

Answers (1)

Greg Beech
Greg Beech

Reputation: 136617

For performance reasons, many of the *Writer classes in .NET buffer their output; that is they do not write straight to the output device but keep some of the data in memory until it hits a threshold size. When you close/dispose the writer, then it knows you are finished with it, and will flush any buffered data to the output.

Here, you create an XmlWriter but you are never closing or disposing it, so it probably still has buffered content that is never flushed, hence the incomplete file. You're also leaving expensive resources open for longer than is necessary by not disposing it.

Surround your instantiations of classes implementing IDisposable with a using block, for example:

using (XmlWriter writer = XmlWriter.Create(rssFileName))
{
    rss2.WriteTo(writer);
}

This will call the Dispose method on the writer at the end of the block, and flush the content. You should also surround the instantiation of XmlReader with a using block.

Upvotes: 1

Related Questions