Background
I need to transform an XML Document to an XHTML document for conversion to a DOCX in a MVC3 web application. I will be merging in paragraph text around the XML data. The Paragraph text is extracted from a DB. In the past I would have certainly use XSLT to transform the XML. However I now realise that Razor provides a very compelling/better alternative. My XSLT is a little rusty now, and I will be using Razor heavily in my MVC application anyway. So is Razor the way to go?
If razor is the way to go then I would grateful as to how one would include this in say the controller. My initial pseudocode thoughts are along the line of:
ViewBag.MyXMLDoc = DocXML;
var MyDocXHtml = View("XHtmlRazorRenderer", ParagraphTextListModel);
Thoughts greatly appreciated.
Edit
MyDocument = MyDocument.LoadXML("MyDocXML.xml")
ViewBag.MyDocument = MyDocument;
var MyDocXHtml = View("XHtmlRazorRenderer", ParagraphTextListModel);
Maybe you can create a ViewModel that mimic the structure of your XML.
That way… you don’t rely on ViewBag… and can loop throught the viewmodel properties and collections to generate the HTML using Razor.
The creation of the viewmodel should be made on the controller, loading the XML and then using xpath load the viewmodel.
Then, in Razor, using the ViewModel you generate the HTML.
Hope your XML is not too complex.
Your ViewModel:
In your Controller just pass the
MyViewModelto the view as the Model.