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?

4

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I have code like this pastebin.com/DtB2m0Cq But I have errors like this imgur.com/a/711bj
Well of course. That's going to return a ContentResult (derived from ActionResult), whereas the method you stuck that into returns IQueryable<TimeTable>. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.