I'm trying to create an XML file to conform to Indeed.com's Job Listing XML.
It looks like:
<?xml version="1.0" encoding="utf-8"?>
<source>
<publisher>Super X Job Site</publisher>
<publisherurl>http://www.superxjobsite.com</publisherurl>
<job>
<title><![CDATA[Sales Executive]]></title>
<date><![CDATA[Fri, 10 Dec 2005 22:49:39 GMT]]></date>
<referencenumber><![CDATA[unique123131]]></referencenumber>
<url><![CDATA[http://www.superxjobsite.com/job/123]]></url>
<company><![CDATA[Big ABC Corporation]]></company>
<city><![CDATA[Phoenix]]></city> <state><![CDATA[AZ]]></state>
<country><![CDATA[US]]></country> <postalcode><![CDATA[85003]]></postalcode>
<description><![CDATA[Some really long job description goes here.]]></description>
</job>
[ more jobs ...]
Now, right now I have a IEnumberable of "Jobs", which has properties which match each one of the XML elements above.
What is the best way to generate this XML document and return it as an ActionResult in ASP.NET MVC?
One way, is I could construct the XML string manually like:
String xmlDoc = "<?xml version="1.0" encoding="utf-8"?>";
xmlDoc += "<source>";
xmlDoc += "<publisher>Super X Job Site</publisher>";
xmlDoc += "<publisherurl>http://www.superxjobsite.com</publisherurl>";
foreach(Job job in Jobs)
{
xmlDoc += "<job>";
xmlDoc += "<description>" + job.Description + "</description>";
...
}
While I know this method would work, is there a better way I should be doing this so I can generate this XML?
You can also accomplish the same task using LINQ to XML.
using System.Xml.Linq;
...
...
...
XDocument xmlDoc = new XDocument(
new XDeclaration("1.0", "utf-16", "true"),
new XElement("source",
new XElement("publisher","Super X Job Site"),
new XElement("publisherurl","http://www.superxjobsite.com")
)
);
foreach (Job job in jobs)
{
xmlDoc.Element("source").Add(
new XElement("job",
new XElement("title", new XCData(job.Title)),
new XElement("date", new XCData(job.Date.ToShortDateString())),
new XElement("referencenumber", new XCData(job.ReferenceNumber)),
new XElement("url", new XCData(job.Url)),
new XElement("company", new XCData(job.Company)),
new XElement("city", new XCData(job.City)),
new XElement("country", new XCData(job.Country)),
new XElement("description", new XCData(job.Description))
)
);
}
If you only need to write it out to a stream. I have had success with the XmlWriter. It is much the same approach as the XmlDocument approach but you can avoid a lot of the CreateElements and AppendElements and generally make things more readable. Below is an example of how you may do it, but you would need to find a better way of doing the cdata as I don't think WriteElementString does it for you.
XmlTextWriter w = new XmlTextWriter(Response.Output);
w.WriteStartElement("source");
w.WriteElementString("publisher", "Super X Job Site");
w.WriteElementString("publisherurl", "http://www.superxjobsite.com");
foreach(Job job in Jobs)
{
w.WriteStartElement("job");
w.WriteElementString("title", "Super X Job Site");
...
w.WriteEndElement();
}
w.WriteEndElement();
w.Close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With