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

如何在C#/ASP.NET MVC中动态生成此XML页面?,c#,asp.net-mvc,xml,C#,Asp.net Mvc,Xml,我正在尝试创建一个XML文件,以符合Required.com的工作清单XML 它看起来像: <?xml version="1.0" encoding="utf-8"?> <source> <publisher>Super X Job Site</publisher> <publisherurl>http://www.superxjobsite.com</publisherurl> <

我正在尝试创建一个XML文件,以符合Required.com的工作清单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 ...] 

超级X工作网站
http://www.superxjobsite.com  
[更多工作…]

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

在ASP.NET MVC中生成此XML文档并将其作为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>";
    ...
}
字符串xmlDoc=”“;
xmlDoc+=”;
xmlDoc+=“超级X工作站点”;
xmlDoc+=”http://www.superxjobsite.com";  
foreach(作业中的作业)
{
xmlDoc+=”;
xmlDoc+=“”+job.Description+“”;
...
}

虽然我知道这个方法会起作用,但是有没有更好的方法来生成这个XML?

您可以使用
System.XML
名称空间来构建它。以下是几个例子:


如果您决定从字符串组装它,请使用
StringBuilder
对象。

如果您只需要将其写入流。我在这方面取得了成功。这与XmlDocument方法基本相同,但您可以避免使用大量CreateElements和AppendElements,并通常使内容更具可读性。下面是一个示例,说明了如何使用cdata,但您需要找到一种更好的方式来使用cdata,因为我认为
WriteElementString
不会为您提供这种方式

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

您还可以使用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用于与XmlDocument的比较:事实上,在许多情况下这更容易(但是如果您在编写时必须通过XPath或其他方式遍历节点,请使用XmlDocument)。