Return xml to asp.net mvc browser view

4

I have the following situation, I need to make an XML file available to be viewed in browser , without the user having to download the file, I do this by saving the file in a directory and then I send the directory of this file and the browser opens as the example below:

public string getXml(int entradaId)
{
    try
    {
        var entrada = ctx.Entradas.Find(entradaId);
        string xml = entrada.Xml;

        var uploadPath = Server.MapPath("~/Content/Uploads");
        string caminhoArquivo = Path.Combine(@uploadPath, entrada.ChaveNota + "-nfe.xml");

        StreamWriter sw = new StreamWriter(caminhoArquivo);
        sw.Write(xml);
        sw.Flush();
        sw.Close();

        return "/Content/Uploads/" + entrada.ChaveNota + "-nfe.xml";
    }
    catch (Exception)
    {
        return "Xml Sem Entrada";
        throw;
    }
}

This works, but I believe it is not right. Well I end up not deleting the file and this will fill the folder with some time.

I need some way to return this XML to the Browser open as a file and make the preview available as an example .

But without my having to save the physical file.

    
asked by anonymous 10.04.2017 / 22:25

2 answers

3

Actually saving the XML just to show in the browser is not a good idea.

You can simply return Content passing as the first parameter to string XML.

public ActionResult GetXml(int entradaId)
{
    var entrada = ctx.Entradas.Find(entradaId);    
    return Content(entrada.Xml, "text/xml");
}   
    
10.04.2017 / 22:35
4

You can create a Custom ActionResult

a> for this.

An example would be:

We'll call our Custom% with% of% with%.

public sealed class XmlActionResult : ActionResult
{
    private readonly XDocument _document;

    public Formatting Formatting { get; set; }
    public string MimeType { get; set; }

    public XmlActionResult(XDocument document)
    {
        if (document == null)
            throw new ArgumentNullException("document");

        _document = document;

        // Default values
        MimeType = "text/xml";
        Formatting = Formatting.None;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.Clear();
        context.HttpContext.Response.ContentType = MimeType;

        using (var writer = new XmlTextWriter(context.HttpContext.Response.OutputStream, Encoding.UTF8) { Formatting = Formatting })
            _document.WriteTo(writer);
    }
}

And in its ActionResult , just do it this way:

public ActionResult Index()
{
    var xml = new XDocument(
            new XElement("root",
                new XAttribute("version", "2.0"),
                new XElement("child", "Hello World!")));

    return new XmlActionResult(xml);
}

If your XmlActionResult is a string, just convert before return, as the example below:

public ActionResult Index()
{
    String xml ="<Root>Root</Root>";

    return new XmlActionResult(XDocument.Parse(xml));
}

Remembering that you can also change the controller to parse if you want.

If you'd like to see other ways, see these questions:

10.04.2017 / 22:48