Tutorial :How can I add line breaks to the serialized output of a SyndicationFeed object?


I'm using the SyndicationFeed class to generate an Atom feed and an Atom10FeedFormatter to serialize it. I'd like to be able to add line breaks between the elements when the file gets written to disk. I realize the feed readers don't care, but when I run my docs through http://feedvalidator.org/ it treats the whole doc as a single line, which makes it a PITA to see where my mistakes are since every error is on "line 1".

For example, instead of output like this:

<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Title For My Feed</title><subtitle type="text">Subtitle for my feed.</subtitle><id>uuid:d2ad3f53-6f1a-4495-ba92-ab3231413f97;id=1</id><updated>2009-05-12T19:42:56Z</updated><author><name>Matt</name>...  

I'd like to get output something like this:

<?xml version="1.0" encoding="utf-8"?>  <feed xmlns="http://www.w3.org/2005/Atom">    <title type="text">Title For My Feed</title>    <subtitle type="text">Subtitle for my feed.</subtitle>    <id>uuid:d2ad3f53-6f1a-4495-ba92-ab3231413f97;id=1</id>    <updated>2009-05-12T19:42:56Z</updated>    <author>      <name>Matt</name>      ...  

Here's the code I'm using to serialize, just in case it matters:

XmlWriter atomWriter = XmlWriter.Create(@"atom.xml");  Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(TheFeed);  atomFormatter.WriteTo(atomWriter);  atomWriter.Close();  


The XmlWriter class has a Settings property that lets you format your xml in a number of ways, including line formatting and also indentation.

Here's the MSDN reference.


Joseph's answer gets the credit for pointing me at the correct class. What follows are the specifics for anyone else looking for teh codez.

All I had to do was tell the XmlWriter object to indent the output. That was easily accomplished by changing the Indent property of the settings object to true. So my original code to serialize the feed (above) was edited like so:

XmlWriterSettings WriterSettings = new XmlWriterSettings();  WriterSettings.Indent = true;    XmlWriter atomWriter = XmlWriter.Create(@"atom.xml", WriterSettings);  Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(TheFeed);  atomFormatter.WriteTo(atomWriter);  atomWriter.Close();  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »