tags:

views:

229

answers:

3

I have an XML document (in a .net C# web app) that I want to display in its simplistic form (similar to IE). I need it to be browser independent (or as much as possible) and have a consistent display.

I have tried pushing the xml document through as "text/xml" and this is inconsistent and doens't work in some browsers.

http://sources.redhat.com/ml/xsl-list/2002-02/msg00831.html A link here provided a good way of transforming the XML to HTML/XHTML using stylesheets. However the stylesheet provided didnt work.

First: Is this the best way to do it? Are there better solutions? Second: If not, does anyone know where I can find the XSLT?

EDIT (clarification): The XSLT I refer to will transform the XML into the Internet Explorer style display of XML files.

Thanks in advance! :)

A: 

I am about to test it but I found this link:

here

EDIT: Found out only works wiht (some) vresions of IE.grr!

Russell
+1  A: 

The stylesheet you are trying to use only works in modern browsers and won't work in any version of IE. Doing such things in modern browsers is trivial but in IE you need to call MSXML through javascript and .NET stuff in order to do the same transformations native to other far more modern browsers (Firefox, Safari, Chrome, Opera, K-Meleon, Konqueror, Epiphany, Flock....you get the idea). This is all part of the problem IE causes by not implementing XHTML which, essentially, is what you want to do and every other browser does.

I'll check back in the morning if you still haven't solved this with javascript I use.

Rob
Thanks Rob - I think I will have to create a simple XSL for my elements specifically if I cant find this utopian document. It would be great to be able to mimick this display and with JQuery and CSS it should be possible to do.
Russell
Turns out, I'm told I can't give out that js code for reasons I can't fathom, but this link is where I found out how to do this:http://msdn.microsoft.com/en-us/library/ms762796(VS.85).aspxand http://msdn.microsoft.com/en-us/library/ms753804(VS.85).aspxI'll give my actual code later after I argue with someone.
Rob
A: 

I decided my best approach (for my situation) was to write 2 simple C# methods to generate HTML from the XML on the server side. This helps reduce any reliance on browser displaying XML (Opera is Crazy!)

(This example uses pretty simple formatting, which was fine for my situation. I loosely modelled it on IE's display of XML. For others' reference, here are the methods:

        /// <summary>
    /// Does a simple convert to display an Xml document in HTML.
    /// </summary>
    /// <param name="xmlString"></param>
    private static string ConvertXmlToHtml(string xmlString)
    {
        StringBuilder html = new StringBuilder();

        html.AppendLine("<HTML>");
        html.AppendLine("<HEAD><TITLE>Xml Document</TITLE></HEAD>");

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlString);

        html.AppendLine(ConvertXmlElementToHTML(1, doc.DocumentElement));

        html.AppendLine("</HTML>");


        return html.ToString();
    }

    /// <summary>
    /// Converts an XML element (and all of its children) to HTML.
    /// This is a recursive method.
    /// </summary>
    /// <param name="element"></param>
    /// <returns></returns>
    private static string ConvertXmlElementToHTML(int level, XmlNode element)
    {
        int padding = level; // padding (cm == level).

        StringBuilder returnHTML = new StringBuilder();
        if (element is XmlElement)
        {
            // Formatting for symbols to simplify code below.
            string close_bracket = "<SPAN style=\"color: blue\">&gt;</SPAN>";
            string close_bracket_no_children = "<SPAN style=\"color: blue\"> /&gt;</SPAN>";
            string open_bracket = "<SPAN style=\"color: blue\">&lt;</SPAN>";
            string open_bracket_end_el = "<SPAN style=\"color: blue\">&lt;/</SPAN>";
            string el_name = "<SPAN style=\"color: brown\">" + element.Name + "</SPAN>";
            string quote = "<SPAN style=\"color: blue\">\"</SPAN>";
            string equals_sign = "<SPAN style=\"color: blue\">=</SPAN>";

            // Open Element.
            returnHTML.AppendLine("<DIV style=\"margin-left: " + padding + "cm\">" + open_bracket + el_name);

            // Print element attributes.
            foreach(XmlAttribute att in element.Attributes)
            {
                returnHTML.AppendLine(" <SPAN style=\"color: brown\">" + att.Name + "</SPAN>" + equals_sign + quote + "<SPAN style=\"color: black; text-weight: bold\">" + att.Value + "</SPAN>" + quote);
            }

            // If no children, we end the element here with a '/ >'
            // otherwise, we close the element and start to write children '>'
            if (element.ChildNodes.Count == 0)
            {
                returnHTML.AppendLine(close_bracket_no_children + "</DIV>");
            }
            else
            {
                returnHTML.AppendLine(close_bracket + "</DIV>");
            }

            // Print Children. (Recursive call). Note location is IMPORTANT: we need child elements
            // to print after the element is opened and before the element is closed.
            foreach (XmlNode child in element.ChildNodes)
            {
                returnHTML.AppendLine(ConvertXmlElementToHTML(level + 1, child));
            }

            // If we have printed child elements, we need to print a closing element tag.
            if (element.ChildNodes.Count > 0)
            {
                returnHTML.AppendLine("<DIV style=\"margin-left: " + padding + "cm\">" + open_bracket_end_el + el_name + close_bracket + "</DIV>");
            }
        }

        // Return a string of HTML that will display elements at this level and below (child nodes).
        return returnHTML.ToString();
    }
Russell
Thanks everyone for your help, this has been an interesting investigation!
Russell