[英]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.