繁体   English   中英

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

[英]How can I generate this XML page dynamically in C# / ASP.NET MVC?

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM