I recently had to create an XML feed for an app I’m working on. The use case is simple. I have a database of jobs and I need to provide an XML feed containing any jobs that were posted in the last week. I’m going to need to do this for several different 3rd party companies that each use a different XML format, so I want to do a little object oriented design to make sure that creating a feed using a new XML format is as easy as possible.
I’ve always heard that LINQ to XML makes working with XML much easier, and I know that MVC2 makes it easy to package up and return any type of data so I’m going to use them. It should make for a pretty painless XML feed. Let’s see.
Starting at the controller
I want to start by writing the consuming code and let that drive the shape of my business layer code. That means I start by writing a Feed action method on one of the controller classes in my MVC2 app. I want to return XML data so i pick a return type of ContentResult, which will allow me to set the content type to “text/xml”. The logic for this method is simple. I have a feedKey that distinctly identifies the 3rd party company / XML schema that I want to generate a feed for. All companies will hit the same action method, but they’ll pass in a different feedKey, which will result in them getting the appropriate XML feed. My action method takes the feedKey as a parameter and then passes it to a factory method on my FeedService class that returns an instance of the appropriate FeedBuilder object. The FeedBuilder is the class that I’ll use to encapsulate logic for getting job data and formatting it as an XML document. I’ll have a different FeedBuilder for each feedKey. The design is a variation on the Strategy pattern. So, back to controller action method. Here’s what it looks like.
// Feed
[AcceptVerbs(HttpVerbs.Get)]
public ContentResult Feed(string feedKey)
{
var feedBuilder = this.FeedService.GetFeedBuilderFor(feedKey);
return this.Content(feedBuilder.GetFeedXml(), "text/xml");
}
The Factory Method
The factory method on my FeedService is pretty straight forward. It just returns an instance of the right FeedBuilder for the feedKey that we pass in to it. Note that the return type is FeedBuilderBase which is an abstract class that defines the shape of a FeedBuilder.
// GetFeedBuilderFor
public virtual FeedBuilderBase GetFeedBuilderFor(string feedKey)
{
switch(feedKey)
{
case FeedKey.SimplyHired:
return new SimplyHiredFeedBuilder();
case FeedKey.Indeed:
return new IndeedFeedBuilder();
default:
return new EmptyFeedBuilder();
}
}
The FeedBuilderBase
Remember we’re using a Strategy pattern. Usually you would define the shape of your strategy class with an interface, but I’m going to use an abstract class because there’s some boilerplate code that will be the same for all of my concrete FeedBuilder classes and I want to implement that code in a single base class. My abstract class is FeedBuilderBase and it looks like this.
public abstract class FeedBuilderBase
{
//**************************************************************************************
// PROPERTIES
//**************************************************************************************
// FeedService
private FeedService _feedService;
public virtual FeedService FeedService
{
get { return NewIfNull(_feedService); }
set { _feedService = value; }
}
//**************************************************************************************
// ABSTRACT METHODS
//**************************************************************************************
// GetFeedData
public abstract List<JobFeedItem> GetFeedData();
// BuildXmlFor
public abstract XDocument BuildXmlFor(List<JobFeedItem> list);
//**************************************************************************************
// HELPER METHODS
//**************************************************************************************
// NewIfNull
public virtual T NewIfNull<T>(T obj) where T : new()
{
if (obj == null) { obj = new T(); }
return obj;
}
// GetFeedXml
public virtual string GetFeedXml()
{
List<JobFeedItem> list = GetFeedData();
XDocument xdoc = BuildXmlFor(list);
var sb = new StringBuilder();
var sw = new StringWriterUtf8(sb);
xdoc.Save(sw);
return sb.ToString();
}
}
I have two abstract methods, GetFeedData and BuildXmlFor. These methods represent the two things that change for each XML feed. Each company has slightly different business rules around what data they want to pull and each company has it’s own proprietary XML format. These 2 methods will have to be overridden whenever we create a concrete FeedBuilder.
You’ll also notice that we implemented GetFeedXml which is the method we called in our action method to get the text of our XML document. GetFeedXml should have been very easy to write, but it took a little time due to an unexpected encoding problem that I described in How to Create XML in C# with UTF-8 Encoding.
The Concrete FeedBuilder
Now I’m ready to create my first concrete FeedBuilder class. I’m going to start with a jobsite aggregator called SimplyHired. So I create a new SimplyHiredFeedBuilder class that inherits from FeedBuilderBase an I override my GetFeedData and BuildXmlFor methods. GetFeedData is just a wrapper for the appropriate data access method in my FeedService class. GetFeedData returns a list of JobFeedItem objects. JobFeedItem is a simple Data Transfer Object (DTO) that I created to contain the aggregate of data needed to build a feed item.
BuildXmlFor is more interesting. It takes a list of JobFeedItem objects returned by our GetFeedData method and transforms it into an XDocument using a LINQ query. The LINQ query is just a select with a bunch of nested XElement constructors that create the XML elements needed to represent each job. We then take the expression created to represent the list of jobs as XElements and we wrap it in a single top level XElement called, wait for it…. jobs. BTW, it is at this point that the LINQ expression actually executes. Up to now it has just been an expression. As soon as we use it in the constructor for our jobs XElement the expression compiles and executes.
So, we now have an XML tree contained in a single XElement called jobs. To complete the method we just wrap the jobs XElement in a new XDocument, return the XDocument, and we’re done.
public class SimplyHiredFeedBuilder:FeedBuilderBase
{
//**************************************************************************************
// FEEDBUILDERBASE OVERRIDES
//**************************************************************************************
// GetFeedData
public override List<JobFeedItem> GetFeedData()
{
return this.FeedService.GetJobFeedItemsForSimplyHired();
}
// BuildXmlFor
public override XDocument BuildXmlFor(List<JobFeedItem> list)
{
var xmlExpression =
from JobFeedItem j in list
select new XElement("job",
new XElement("title", j.JobTitle),
new XElement("job-code", j.JobGuid),
new XElement("job-board-name", j.CompanyName),
new XElement("job-board-url", j.CompanyJobPageUrl),
new XElement("detail-url", ""),
new XElement("apply-url", GetApplyUrl(j.CompanyKey, j.JobGuid)),
new XElement("job-category", j.JobCategory),
new XElement("description",
new XElement("summary", j.JobDescription),
new XElement("required-skills", ""),
new XElement("required-education", ""),
new XElement("required-experience", ""),
new XElement("full-time", j.IsFullTime),
new XElement("part-time", j.IsPartTime),
new XElement("flex-time", ""),
new XElement("internship", j.IsInternship),
new XElement("volunteer", ""),
new XElement("exempt", ""),
new XElement("contract", j.IsContract),
new XElement("permanent", j.IsPermanent),
new XElement("temporary", j.IsTemp),
new XElement("telecommute", "")
),
new XElement("compensation",
new XElement("salary-range", ""),
new XElement("salary-amount", ""),
new XElement("salary-currency", ""),
new XElement("benefits", "")
),
new XElement("posted-date", GetPostedOnDate(j)),
new XElement("close-date", GetClosedOnDate(j)),
new XElement("location",
new XElement("address", ""),
new XElement("city", j.JobCity),
new XElement("state", j.JobState),
new XElement("zip", ""),
new XElement("country", "")
),
new XElement("contact",
new XElement("name", j.ContactName),
new XElement("email", j.ContactEmail),
new XElement("hiring-manager-name", ""),
new XElement("hiring-manager-email", ""),
new XElement("phone", j.ContactPhone),
new XElement("fax", "")
),
new XElement("company",
new XElement("name", j.CompanyName),
new XElement("description", j.CompanyDescription),
new XElement("industry", ""),
new XElement("url", j.CompanyUrl)
)
);
var jobs = new XElement("jobs", xmlExpression);
var declaration = new XDeclaration("1.0", "utf-8", null);
return new XDocument(declaration, jobs);
}
}
Conclusion
So I built it, tested it, and it works. Except for the hour that I spend trying to figure out my UTF-8 encoding problem, the LINQ / XElement method was a relatively painless way to create XML. I love the simplicity of the MVC action method and the fact that it gives me a ContentResult type that gives me access the right parts of the stack. The LINQ / XElement query was concise and the style of passing element data in XElement constructors makes it hard to mess up the document structure. My conclusion is that I like it and I’ll definitely be using this technique again.