user3638312
user3638312

Reputation: 11

how to load xml from url and apply specified xsl (xml-stylesheet processing instruction)

I am using C# to load a url. My URL is an XML document which contains the processing instruction<?xml-Stylesheet type="text/xsl" href=".\CS_Xml_Output.xsl"?>

During development, I was using SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer(); ie.Navigate2(...). But since deploying to the server, I am getting the error.

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {0002DF01-0000-0000-C000-000000000046} failed due to the following error: 80070005.

As per above advice, I tried the following. In both cases I am receiving the raw XML. How do I get get the transformed HTML? (NOTE: I cannot use XSLCompiledTransform because the 3rd-party provided XSL uses XSLT only supported by MSXML.)

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead(xmlUrl);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();

as well as

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(URL.ToString());

using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
{
    Stream receiveStream = httpResponse.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

    StreamReader readStream = new StreamReader(receiveStream, encode);

    Console.WriteLine("\r\nResponse stream received.");
    Char[] read = new Char[256];
    int count = readStream.Read(read, 0, 256);

    Console.WriteLine("HTML...\r\n");

    while (count > 0)
    {
        String str = new String(read, 0, count);
        Console.Write(str);
        baseRequest.Append(str);
        count = readStream.Read(read, 0, 256);
    }

    etc....

}

Upvotes: 0

Views: 1348

Answers (3)

mtnmuncher
mtnmuncher

Reputation: 1

FWIW, I ended up calling an external JScript as I could not get MSXML 6 to convert inside C# without error.

C# code

    private string LaunchJScript(string xmlFilename, string xslFilename)
    {

        //string xslLocation = System.Web.HttpContext.Current.Server.MapPath("~/transform");
        string htmlFilename = xmlFilename.Substring(0, xmlFilename.LastIndexOf(".") + 1) + "html";


        // Start the child process.
        Process process = new Process();
        // Redirect the output stream of the child process.
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.RedirectStandardOutput = false;
        process.StartInfo.FileName = xslLocation + "\\" + "xsltest.js";
        process.StartInfo.Arguments = xmlFilename + " " + xslFilename + " " + htmlFilename;

        process.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        // string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        if (File.Exists(htmlFilename))
        {
            _Logger.InfoFormat("Converted file is {0}", htmlFilename);
            return htmlFilename;
        }
        else
        {
            _Logger.ErrorFormat("Converted file {0} does not exist", htmlFilename);
            return string.Empty;
        }
    }

JScript code

var oArgs = WScript.Arguments;

if (oArgs.length == 0) { WScript.Echo ("Usage : cscript xslt.js xml xsl html"); WScript.Quit(); } xmlFile = oArgs(0); xslFile = oArgs(1); htmlFile = oArgs(2);

var xsl = new ActiveXObject("MSXML2.DOMDOCUMENT.6.0"); var xml = new ActiveXObject("MSXML2.DOMDocument.6.0");

xml.async = false xml.resolveExternals = true

xsl.async = false xsl.resolveExternals = true xsl.setProperty("AllowDocumentFunction", true) xsl.setProperty("AllowXsltScript", true)

xml.validateOnParse = false; xml.async = false; xml.load(xmlFile);

if (xml.parseError.errorCode != 0) WScript.Echo ("XML Parse Error : " + xml.parseError.reason);

xsl.async = false; xsl.load(xslFile);

if (xsl.parseError.errorCode != 0) WScript.Echo ("XSL Parse Error : " + xsl.parseError.reason);

try { var result = xml.transformNode(xsl.documentElement) var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile(htmlFile, true); a.WriteLine(result); a.Close();

} catch(err) { WScript.Echo("Transformation Error : " + err.number + "*" + err.description); }

Upvotes: 0

Orion Edwards
Orion Edwards

Reputation: 123652

If you MUST use MSXML because of the strange markup, then you have to use COM interop to load the MSXML library, and then call it's xsl processing functions.

Start by firing up visual studio, doing an Add Reference on your project, and find "Microsoft XML, v3.0" under the COM tab :-)

Upvotes: 0

John Saunders
John Saunders

Reputation: 161783

That processing instruction is for IE or another browser. Only a browser is going to interpret it.

Upvotes: 0

Related Questions