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

时间:2009-11-05 04:47:43

标签: c# asp.net-mvc xml

我正在尝试创建一个符合Indeed.com的Job Listing XML的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 ...] 

现在,我现在有一个IEnumberable的“Jobs”,它的属性与上面的每个XML元素相匹配。

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

一种方法是,我可以手动构建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吗?

4 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

如果您只需要将其写入流中。我在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();

答案 2 :(得分:2)

您可以使用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对象。

答案 3 :(得分:0)

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);