簡體   English   中英

創建自定義xml(ASP.NET MVC)

[英]Create custom xml (ASP.NET MVC)

我有方法從數據庫中獲取所有值

這是代碼

public IQueryable<TimeTable> GetTimeTables()
    {
        return db.TimeTables;
    }

這是時間表的模型

public partial class TimeTable
{
    public int Id { get; set; }
    public string Company { get; set; }
    public string INN { get; set; }
    public string StartDay { get; set; }
    public string StartPause { get; set; }
    public string EndDay { get; set; }
    public string EndPause { get; set; }
}

我需要從這樣的值生成xml

<data>
<worker id="000000000000">
    <start>2016-08-08T08:00:00</start>
    <pause>2016-08-08T13:15:49</pause>
    <continue>2016-08-08T13:15:49</continue>
    <end>2016-08-08T13:15:49</end>
</worker>
<worker id="000000000001">
    <start>2016-08-08T08:00:00</start>
    <pause>2016-08-08T13:15:49</pause>
    <continue>2016-08-08T13:15:49</continue>
    <end>2016-08-08T13:15:49</end>
</worker>

其中id是INN, start是StartDay, pause是StartPause, continue是EndPause, end是EndDay。

我該怎么做?

這很簡單,所以我不確定您到底在哪里遇到問題。 本質上,您只需要構建一個XDocument例如:

var xdoc = new XDocument(
    new XElement("data",
        timeTables.Select(w =>
            new XElement("worker",
                new XAttribute("id", w.INN),
                new XElement("start", w.StartDay),
                new XElement("pause", w.StartPause),
                new XElement("continue", w.EndPause),
                new XElement("end", w.EndDay)
            )
        )
    )
);

然后,您只需使用MIME類型將其作為ContentResult返回:

return Content(xdoc.ToString(), "application/xml", Encoding.UTF8);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM