如何在C#/ ASP.NET MVC中动态生成此XML页面?

问题描述:

我正在尝试创建一个XML文件以符合Indeed.com的JobList XML。如何在C#/ ASP.NET MVC中动态生成此XML页面?

它看起来像:

<?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 ...] 

现在,现在我有“乔布斯”,其中有上面的XML元素的每一个匹配的属性的IEnumberable。

生成此XML文档并将其作为ActionResult在ASP.NET MVC中返回的最佳方式是什么?

的一种方式,是我可以手动构造XML字符串,如:

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>"; 
    ... 
} 

虽然我知道这种方法会工作,有没有更好的办法,我应该这样做,这样我就可以产生这个XML?

您还可以使用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)) 
      ) 
     ); 
    } 

您可以使用System.Xml命名空间来构建它。这里有几个例子:

http://www.csharphelp.com/archives/archive199.html

http://paulsiu.wordpress.com/2007/04/04/creating-a-xml-document-from-scratch-without-using-a-file-in-c/

如果你决定从字符串进行组装,使用StringBuilder对象,而不是。

+0

抢妳onfire – 2009-11-05 04:56:33

using System.Xml; 

... 

XmlDocument doc = new XmlDocument(); 
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "utf-8", null); 
doc.AppendChild(docNode); 
XmlNode source = doc.CreateElement("source"); 

XmlNode publisher = doc.CreateElement("publisher"); 
publisher.InnerText = "Super X Job Site"; 
source.AppendChild(publisher); 

XmlNode publisherUrl = doc.CreateElement("publisherurl"); 
publisherUrl.InnerText = "http://www.superxjobsite.com"; 
source.AppendChild(publisherUrl); 

foreach(Job job in Jobs) 
{ 
    XmlNode jobNode = doc.CreateElement("job"); 
    ... 
    source.AppendChild(jobNode); 

} 

doc.AppendChild(source); 
+0

我会删除我的帖子,很明显,几秒钟内,我们有同样的想法,虽然你犯了一个好得多的例子:) – Abel 2009-11-05 04:57:03

如果您只需要将它写出到流中。我已经与XmlWriter成功。这与XmlDocument方法非常相似,但您可以避免大量的CreateElements和AppendElements,并且通常使事情更具可读性。下面是你如何做的一个例子,但是你需要找到一个更好的方式来做cdata,因为我不认为WriteElementString会为你做。

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(); 
+0

+1与XmlDocument的比较:的确更容易在很多情况下(但是如果您必须遍历 - 使用XPath或其他方式 - 写入时节点,尽管使用XmlDocument)。 – Abel 2009-11-05 13:03:53