How to load xml content in xml file

Possible duplicate:
How to upload xml file to asp.net using c #

can someone help how to load xml (which is in line). 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 it does not work. On error

Error:

The XML document must have a top-level element. Error handling resource

Thanks,

+4
source share
1 answer

You don't need an 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); } 
+6
source

All Articles