3

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);

2 Answers 2

2

I'd stick with XSLT for the given job.

Note that Razor is a "generic" text templating engine, and it will do nothing to ease the generation of correct XML. Also, the traversal of complex XML with namespaces is IMHO much more natural and concise with XPath compared to LINQ-to-XML.

It's not too hard to generate a custom view engine that does the job of performing XSLT just like a Razor template renders text and HTML. This allows for a nice and natural integration of the XSLT rendering in the scope of the ASP.NET MVC application.

Sign up to request clarification or add additional context in comments.

4 Comments

Lucero, Thanks for the rapid reply. My XML will be pretty complex, although I do have the option of loading the XML document into a class structure using XSD2Code. I then have Document model and ParagraphTextList Model, both in Classes. See Edit above for new code. I forgot that I could do this. It may make a difference?
It is a shame one cannot load in more than one model, and instead have to rely on the ViewBag for passing in one of the models.
@SamJolly, no, that doesn't make a big difference IMHO. Complex XML will result in complex object hierarchies and those are not ideal as view models. That being said, you can of course pass more than one model by using a small helper class (or just Tuple<>) as the model, which then exposes your multiple parts as properties.
Lucero, a big thank you. I wanted to give you points as well, but I cannot as I am a young account. However your help was extremely appreciated.
0

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:

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

In your Controller just pass the MyViewModel to the view as the Model.

4 Comments

Hi, Thanks for this. I guess I am already effectively creating a View/Document model by using the XSD2Code component which create a class structure from an XML Schema, and therefore can load an XML file that complies with this schema into an instantiation of this class. The problem is that I have two classes, one for the Document Model and one for the paragraph text which is pulled from the DB.
Your ViewModel, could have 2 properties... one for the XMLDocument, and another for the Paragraph. See the edit.
Hi Romias, Of course the VM can have 2 properties with each containing a complex class structure. I keep forgetting this. I assume putting data into objects as opposed to keeping it in XML will have performance and memory saving benefits as well. I am very aware that XML documents can get large and verbose, and on a multiuser web application one is not wishing to fill up the RAM with bloaty data structures. Thanks.
Thanks, really appreciate the advice, and insight into ViewModels. I can use this since I am using the XSD2Code component to put XML into a class structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.