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,
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.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With