Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

download xml file asp.net web api

public class OkXmlDownloadResult : IHttpActionResult
{
    private readonly ApiController _controller;

    public OkXmlDownloadResult(string xml, string downloadFileName,
        ApiController controller)
    {
        if (xml == null)
        {
            throw new ArgumentNullException("xml");
        }

        if (downloadFileName == null)
        {
            throw new ArgumentNullException("downloadFileName");
        }

        if (controller == null)
        {
            throw new ArgumentNullException("controller");
        }

        Xml = xml;
        ContentType = "application/xml";
        DownloadFileName = downloadFileName;
        _controller = controller;
    }

    public string Xml { get; private set; }

    public string ContentType { get; private set; }

    public string DownloadFileName { get; private set; }

    public HttpRequestMessage Request
    {
        get { return _controller.Request; }
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(Execute());
    }

    private HttpResponseMessage Execute()
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StringContent(Xml);
        response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(ContentType);
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = DownloadFileName
        };
        return response;
    }
}

// Use this below code in controller 
public class ValuesController : ApiController
{
    public IHttpActionResult Get()
    {
        User user = new User()
        {
            FirstName = "First",
            LastName = "Last"
        };

        // Alternative 1
        XmlSerializer serializer = new XmlSerializer(typeof(User));

        // Alternative 2
        // DataContractSerializer serializer = new DataContractSerializer(typeof(User));

        StringBuilder builder = new StringBuilder();
        using (StringWriter writer = new StringWriter(builder))
        {
            serializer.Serialize(writer, user);

            // alternative 2
            // serializer.WriteObject(writer, user);
        }

        // create XML from your data.
        return new OkXmlDownloadResult(builder.ToString(), "myfile.xml", this);
    }
}

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: activeNetworkInfo depricated 
Csharp :: invalid length for a base-64 char array or string. frombase64string c#Add Answer 
Csharp :: c# with keyword 
Csharp :: f# list map function 
Csharp :: c# gridview summary item displayformat 
Csharp :: unity exenerate 
Csharp :: asp.net mvc hide div from controller 
Csharp :: snakes and ladder single player c# 
Csharp :: alpahbet incremnet in c# 
Csharp :: Devexpress MVC Gridview BinaryImage Picture 
Csharp :: missing integer c# 
Csharp :: C# if with obj params 
Csharp :: get current culture in controller asp.net core 
Csharp :: Get dwgexport setting reivit api 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: c# sprintf equivalent 
Csharp :: c# get buttons row and column in grid 
Csharp :: kentico 13 api save attachment 
Csharp :: touch screen to world point 
Csharp :: uity pause game 
Csharp :: datagrid drop file wpf mvvm example 
Csharp :: regex ip rage detect 
Csharp :: c# properties making string required 
Csharp :: unity follow object 
Csharp :: c# unary operator 
Csharp :: spreate by captial char in c# 
Csharp :: Fibonacci Ienumerable 
Csharp :: csharp functions 
Csharp :: c# register write value 
Csharp :: Runtime.getRuntime().addShutdownHook(printingHook); c# 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =