Atom.NET Enhancement
I was reviewing the caching implementation for this site and I wanted to cache my Atom feed as an XmlDocument that I could write to the response stream. I needed to make a couple of changes to the AtomWriter class so I could create an XmlDocument. I thought about using a memory stream, but my RSS feed already deals with XmlDocuments and I wanted to share the caching logic.
The result of these changes make it possible to pass an XmlNodeWriter to the AtomFeed Save method. So, we get something like this:
... XmlDocument d = new XmlDocument(); XmlNodeWriter xnw = new XmlNodeWriter(d, false); feed.Save(xnw); xnw.Close(); ...
The changes are reproduced below (look for Removed in the source code).
internal class AtomWriter
{
private XmlWriter _writer = null;
#region Constructors
internal AtomWriter(TextWriter w)
{
this._writer = new XmlTextWriter(w);
Init();
}
internal AtomWriter(Stream w, Encoding encoding)
{
this._writer = new XmlTextWriter(w, encoding);
Init();
}
internal AtomWriter(string filename, Encoding encoding)
{
this._writer = new XmlTextWriter(filename, encoding);
Init();
}
internal AtomWriter(XmlWriter writer)
{
// Removed this._writer = (XmlTextWriter)writer;
this._writer = writer;
Init();
}
#endregion Constructors
#region Private methods
private void WriteHeader()
{
this._writer.WriteStartDocument();
this._writer.WriteComment(DefaultValues.GeneratorMessage);
// Removed this._writer.WriteRaw(Environment.NewLine);
}
///
/// Writes the Atom feed body.
///
private void WriteFeed(AtomFeed feed)
{
if(this._writer == null)
throw new InvalidOperationException("AtomWriter has been closed, and can not be written to.");
this.WriteHeader();
if(feed == null)
throw new RequiredElementNotFoundException("AtomFeed cannot be null.");
this._writer.WriteRaw(feed.ToString());
// finalize the writer
this._writer.Flush();
this._writer.Close();
this._writer = null;
}
///
/// Writes the Atom entry body.
///
private void WriteEntry(AtomEntry entry)
{
if(this._writer == null)
throw new InvalidOperationException("AtomWriter has been closed, and can not be written to.");
if(entry == null)
throw new RequiredElementNotFoundException("AtomEntry cannot be null.");
this._writer.WriteRaw(entry.ToString());
this._writer.Flush();
this._writer.Close();
this._writer = null;
}
private void Init()
{
// Removed
//this._writer.Formatting = Formatting.Indented;
//this._writer.Indentation = 2;
}
#endregion Private methods
#region Internal methods
internal void Write(AtomFeed feed)
{
this.WriteFeed(feed);
}
internal void Write(AtomEntry entry)
{
this.WriteEntry(entry);
}
#endregion Internal methods
}
There are no comments.