Razor instead of XSLT to transform XML document into MVC3 / MVC4 application?

Background

I need to convert XML document to XHTML document to convert to DOCX in MVC3 web application. I will be concatenating paragraph text around XML data. The paragraph text is retrieved from the database. In the past, I would of course use XSLT to transform XML. However, I now understand that Razor provides a very attractive / better alternative. Now my XSLT is a little rusty and I will still be using Razor in my MVC application. So what's the Razor way to go?

If a shaver is the way to go, I would appreciate how this could be included in the controller. My initial pseudo code thoughts are along the line:

  ViewBag.MyXMLDoc = DocXML;    
  var MyDocXHtml = View("XHtmlRazorRenderer", ParagraphTextListModel);

      

Thoughts were highly appreciated.

Edit

MyDocument = MyDocument.LoadXML("MyDocXML.xml")    
ViewBag.MyDocument = MyDocument;
var MyDocXHtml = View("XHtmlRazorRenderer", ParagraphTextListModel);

      

+3


source to share


2 answers


Perhaps you can create a ViewModel that mimics the structure of your XML.

This way ... you don't rely on the ViewBag ... and can quote the viewmodel properties and collections to generate HTML with Razor.

The creation of the viewmodel must be done on the controller, load the XML and then use xpath to load the viewmodel.

Then in Razor, using the ViewModel, you create HTML.



Hope your XML is not too complicated.

Your ViewModel:

public class MyViewModel{
   public ParsedXMLDoc myXmlData {get; set;}
   public ParagraphTextListModel paragraph {get; set;}
}

      

In your controller, just pass MyViewModel

to the view as a model.

0


source


I would stick with XSLT for this assignment.

Note that Razor is a "generic" templating engine, and does nothing to facilitate the generation of valid XML. Also, traversing complex XML with namespaces is IMHO much more natural and concise with XPath compared to LINQ-to-XML.



It's not that hard to create a custom view engine that does the work of executing XSLT in the same way that a Razor template renders text and HTML. This allows nice and natural integration of XSLT rendering within an ASP.NET MVC application.

+2


source







All Articles