tags:

views:

518

answers:

5

I need to generate an XML file in C#.

I want to write the code that generates this in a file that is mostly Xml with code inside of it as I can in an ASP.NET MVC page.

So I want a code file that looks like:

<lots of angle brackets...>

  <% foreach(data in myData)
     { %>

     < <%= data.somefield %>

  <% } %>

More angle brackets>

This would generate my XML file. I would not mind using part of System.Web if someone tells me how I can do it without IIS overhead or kludging a generation of a web file.

I want to use templating and I want templating that is similar to ASP.NET

A: 

Create a PageView in a standard ASPX file, but don't include a master or anything else. Just start putting in the angle brackets and everything else. The one thing you will need to do is set the content type. But that can be done in your action by calling Response.ContentType = "text/xml";

Nick Berardi
A: 

There is an XSLT View Enging in MvcContrib here: http://mvccontrib.googlecode.com/svn/trunk/src/MvcContrib.XsltViewEngine/

This can probably give you what you need.

(any of the view engines will work, actually... though the WebForms view engine will complain that what you're writing isn't valid HTML.

Ben Scheirman
+2  A: 

First off, its MUCH easier to generate XML using XElements. There are many examples floating around. Just search for "Linq to XML."

Alternatively, if you absolutely need to do templating, I'd suggest using a template engine such as NVelocity rather than trying to kludge ASP.NET into doing it for you.

Will
A: 

The simplest way of doing this from code would be using the XMLWriter class in System.Xml.

Tutorial here.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("    ");
using (XmlWriter writer = XmlWriter.Create("books.xml", settings))
{
    // Write XML data.
    writer.WriteStartElement("book");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    writer.Flush();
}
Sklivvz
A: 

Further to the above - use the new XML classes delivered with Linq - they make generation of XML in a logical fashion much much easier though you won't get down to something akin to a template in C#.

If you really need something template like then - and I know that this won't necessarily go down well - you should look at doing this part of the system in VB.NET which does have explicit support for template like XML generation as part of its Linq to XML implementation. At the very least you should look at what VB.NET offers before dismissing it.

The nature of .NET means that you don't have to use VB.NET for anything else, you can limit it to the class(es) necessary to do the XML generation and its "generic" in the sense that it comes with .NET and the surrounding logic should be comprehensible to any competent .NET programmer.

Murph
Love to know why this is downvoted because its a carefully qualified and SENSIBLE answer to the question for a competent .NET developer
Murph