tags:

views:

1510

answers:

5

I have an XML document and a CSS file that goes with it, which includes the page formatting style (both exported from Adobe Framemaker). I would like to import this data and display portions of the XML document in a web browser control with in Windows Forms or WPF. It's not clear to me how do make all of this work together.

Any suggestions would be helpful, thanks.

+3  A: 

The approach I took was to get an xslt translation that will format the raw xml into html encoded content for the browser. A google search for "xml pretty print" will find you an xslt transform ready to go.

Use the XML control to do the rest. Give it your xslt file, and the xml and it will take care of transforming the xml.

kareem
<i>A google search for "xml pretty print" will find you an xslt transform ready to go</i>My google-fu must be weak. Searching for that is turning me up a number of odd Perl and Java code snippets showing "pretty" XML but not HTML-encoded XML. Might you have a more specific link? Thanks.
Jesse C. Slicer
Nevermind, got something that uses Prototype.
Jesse C. Slicer
A: 

You can't use XML with CSS you can only use CSS with HTML (or XHTML).

If the XML file is XHTML you need to add a reference to the CSS inside the head elements:

<link href="mycss.css" type="text/css" rel="stylesheet" />

If the XML file is not XHTML you have to transform it into HTML (and than add the link to the css).

As kareem said you can use google to find the appropriate XSLT code, here is some code I have to use XSLT in C#:

XmlDocument source = new XmlDocument();
source.Load(xmlFilePath);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltFilePath));
XmlWriterSettings settings = new XmlWriterSettings();
XmlWriter dest = XmlWriter.Create(htmlFilePath, settings);
xslt.Transform(source, dest);
dest.Flush();
dest.Dispose();

You can than open the file at "htmlFilePath" in a web browser control.

Nir
You can use a CSS with an XML file... (http://www.w3schools.com/Xml/xml_display.asp)
Russell
A: 

can this be done without saving it to the hard drive?

A: 

To do it in memory change the end of Nir's example to do something like this:

StringBuilder sb = new StringBuilder(2500);
XmlWriterSettings settings = new XmlWriterSettings();
XmlWriter dest = XmlWriter.Create(sb, settings);
xslt.Transform(source, dest);;
MessageBox.Show(sb.ToString());
Dror Harari
+1  A: 
webBrowser1.NavigateToStream(parseXmlToHtml(report));

private Stream parseXmlToHtml(string xmlDocument)
{           
   XmlDocument dat = new XmlDocument();
   XslCompiledTransform xslt = new XslCompiledTransform();

   MemoryStream outputStream = new MemoryStream();
   XmlTextWriter writer = new XmlTextWriter (outputStream, System.Text.Encoding.ASCII);

   dat.LoadXml(xmlDocument);  // or dat.Load("c:\\dat.xml");
   xslt.Load("c:\\MonthlyLate.xslt");
   xslt.Transform(dat, writer);

   outputStream.Position = 0;

   return outputStream;
}
Ihab