0

I have method to get all values from database

Here is code

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

Here is model of TimeTable

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

I need to generate xml from values like this

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

Where id is INN, start is StartDay, pause is StartPause, continue is EndPause, end is EndDay.

How I can do this?

mark_spencer
  • 393
  • 4
  • 17
  • return on action@alex – mark_spencer Aug 14 '17 at 17:04
  • How about create your own method to generate XDocument with requred fields? I understood, that's a problem using standart [Serializable()] – KamikyIT Aug 14 '17 at 17:05
  • Possible duplicate of [Return XML from a controller's action in as an ActionResult?](https://stackoverflow.com/questions/134905/return-xml-from-a-controllers-action-in-as-an-actionresult) – Vanity Slug - codidact.com Aug 14 '17 at 17:06
  • If you are using SQL Server as the db engine, you can easily format the XML using the FOR XML and just return the string in your controller https://learn.microsoft.com/es-es/sql/relational-databases/xml/for-xml-sql-server – Victor Hugo Terceros Aug 14 '17 at 17:09

1 Answers1

2

This is pretty straight-forward, so I'm not sure where exactly you're running into issues. Essentially, you just need to build an XDocument like:

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

Then, you just return this as a ContentResult with a mime type:

return Content(xdoc.ToString(), "application/xml", Encoding.UTF8);
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I have code like this https://pastebin.com/DtB2m0Cq But I have errors like this http://imgur.com/a/711bj – mark_spencer Aug 14 '17 at 17:37
  • Well of course. That's going to return a `ContentResult` (derived from `ActionResult`), whereas the method you stuck that into returns `IQueryable`. This code should be in your *action*. The `timeTables` variable would be the result of `GetTimeTables()`, and then the return would be for the action method. Alternatively, you could move the XML creation logic into your `GetTimeTables()` method, but you should then return either `XDocument` or `string` from that method. The `return Content` bit would still be part of your action. – Chris Pratt Aug 14 '17 at 17:51