Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML in C# - Read from Resources, Manipulate and Display

Tags:

c#

xml

I'd like to do the following and can't find an elegant way:

  1. Read an XML template into a System.Xml.XmlDocument
  2. Populate it with data from my UI
  3. Transform it with an XSLT I've written
  4. Apply a CSS Stylesheet
  5. Render it to a WebBrowser control

I'm currently reading it from a file on disk, populating it, then saving it back out to disk after populating it. I reference the XSLT in the template, and the CSS in the XSLT and then use the WebBrowser.Navigate([filename]) method to display the XML file.

Obviously, when I come to deploy this app, it'll break horribly as the file won't exist on disk, and I won't be able to reference the XSLT and CSS files in the XML file as they'll be resources. I'm planning to include the template as a resource, but can't find a neat way to proceed from there.

Any help much appreciated

like image 240
Jon Artus Avatar asked Nov 06 '22 23:11

Jon Artus


2 Answers

Quick question. Why do you need an Xml template? If you already know the schema before hand, then simply generate the complete Xml in your code. There shouldn't be a need for loading a template file.

like image 149
Vaibhav Avatar answered Nov 15 '22 05:11

Vaibhav


Thanks for the link Keith, I'll have a look at that out of interest, as LINQ is on my list of things to learn. Unfortunately, I need to target .Net 2.0 for this app, so I think (please correct me if I'm wrong!) that LINQ is out of the question.

I've now included the CSS in the header of the XSLT, and I've found a way to use a System.Xml.Xsl.XslCompiledTransform object to transform the XML in memory. I'm currently using the WebBrowser.DocumentText property to pass the formatted XML into the WebBrowser componenet and this seems to work.

I can't help thinking that this isn't the best way of doing things, so any comments on better ways would be appreciated. In particular, if I were using LINQ, would I need a schema to bind to, and also, should I have a schema full-stop? I'm not great with XML, but I like Vaibhav's idea of generating straight from a schema rather than using a template. I'm just not sure where to start, so any pointers appreciated!

like image 40
Jon Artus Avatar answered Nov 15 '22 05:11

Jon Artus