I’m trying to generate an XML document from C# List. The xml document must adhere to third party XML Schema.
I’ve had a look at XSD2CODE tool but did not get anywhere.
Could someone please help me with how to generate an XML file using LINQ to xml and return XML file using MVC3.
LINQ to XML is used for querying XML, not generating. If you want to generate an XML file there are other techniques:
But no matter which XML generation technique you choose to adopt I would recommend you writing a custom action result that will perform this job instead of polluting your controller with XML generation plumbing which is absolutely not its responsibility.
Let’s have an example using the third approach (
XmlSerializer).Suppose that you have defined a view model that you want to convert to XML:
Now we could write a custom action result that will perform this conversion:
and the controller action will be pretty standard:
You might also want to check the ASP.NET MVC 4 Web API which makes those scenarios pretty easy.