Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a xml content in a xml file [duplicate]

Possible Duplicate:
How to download xml file in asp.net using C#

can anyone please help how to download the xml (which is in string).I am using MVC3

Mycode;

 public FileResult Download(string id)
    {  
        var model = service.GetAllDefinitions().First(x => x.ID == id);
        var definitionDetails = new StatisticDefinitionModel(model);
        string xmlString = definitionDetails.ToXml;
     //string presented xml  

        string fileName = definitionDetails.Name + ".xml";

            var stream = new MemoryStream();

            var writer = XmlWriter.Create(stream);
            writer.WriteRaw(xmlString);
            stream.Position = 0;
            var fileStreamResult = File(stream, "application/xml", fileName);
            return fileStreamResult; 

    }

but this not working.gives an error

Error:

XML document must have a top level element. Error processing resource

Thanks,

like image 300
Ash Avatar asked Aug 08 '12 11:08

Ash


People also ask

Can we import XML file into another XML file?

An XML external entity cannot be a full-blown independent XML document—neither standalone XML declaration nor Doctype declaration is allowed. That effectively means an XML external entity itself cannot include other external entities.

How do I download XML in Chrome?

Simply click the File button (the 3 lines), and click Save Page As. For example, I went to xml-sitemaps.com/sitemap.xml and clicked Save Page As. It saved as XML to my local machine and loaded as such. Without any HTML.


1 Answers

You don't need a xml stream here, just return the bytes.

public FileResult Download(string id)
{  
    var model = service.GetAllDefinitions().First(x => x.ID == id);
    var definitionDetails = new StatisticDefinitionModel(model);
    string xmlString = definitionDetails.ToXml;
    string fileName = definitionDetails.Name + ".xml";


    return File(Encoding.UTF8.GetBytes(xmlString), "application/xml", fileName);
}
like image 64
Marcelo De Zen Avatar answered Sep 25 '22 09:09

Marcelo De Zen