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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markpittsnh
    New Member
    • Jan 2010
    • 4

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

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

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

    System.Unauthor izedAccessExcep tion: 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 XSLCompiledTran sform 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(da ta);
    string s = reader.ReadToEn d();

    as well as

    HttpWebRequest httpRequest = (HttpWebRequest )WebRequest.Cre ate(URL.ToStrin g());
    using (HttpWebRespons e httpResponse = (HttpWebRespons e)httpRequest.G etResponse())
    {
    Stream receiveStream = httpResponse.Ge tResponseStream ();
    Encoding encode = System.Text.Enc oding.GetEncodi ng("utf-8");
    StreamReader readStream = new StreamReader(re ceiveStream, encode);
    Console.WriteLi ne("\r\nRespons e stream received.");
    Char[] read = new Char[256];
    int count = readStream.Read (read, 0, 256);
    Console.WriteLi ne("HTML...\r\n ");
    while (count > 0)
    {
    String str = new String(read, 0, count);
    Console.Write(s tr);
    baseRequest.App end(str);
    count = readStream.Read (read, 0, 256);
    }
    etc....

    }

    Cheers!
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi markpittsnh,

    Try using this:

    Code:
    using System.Xml;
    
    XmlDocument doc = new XmlDocument();
    doc.Load(string yourDocumentUrl);
    Or you could use the HttpWebRequest and load the XML content into a string, in which case you'd replace doc.Load() with doc.LoadXml(str ing xmlContent).

    Hope this helps,

    codegecko

    Comment

    • markpittsnh
      New Member
      • Jan 2010
      • 4

      #3
      how to load xml from url and apply specified xsl

      Hey codegecko,

      Using this approach.....

      XmlDocument doc = new XmlDocument();
      doc.Load("http://localhost:6713/temp.xml");
      ...if I perform a quick watch from VS, all I see if the XML. I am trying to find a way for the inline xsl to be applied. My original approach was using the internetexplore r object. But this doesn't play nice on a server.

      Regarding the use of HttpWebRequest, please have a look at the snippet I posted earlier. Is your suggestion any different than this?

      Cheers!

      Comment

      • markpittsnh
        New Member
        • Jan 2010
        • 4

        #4
        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(s tring xmlFilename, string xslFilename)
        {

        //string xslLocation = System.Web.Http Context.Current .Server.MapPath ("~/transform");
        string htmlFilename = xmlFilename.Sub string(0, xmlFilename.Las tIndexOf(".") + 1) + "html";


        // Start the child process.
        Process process = new Process();
        // Redirect the output stream of the child process.
        process.StartIn fo.UseShellExec ute = true;
        process.StartIn fo.RedirectStan dardOutput = false;
        process.StartIn fo.FileName = xslLocation + "\\" + "xsltest.js ";
        process.StartIn fo.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.Standar dOutput.ReadToE nd();
        process.WaitFor Exit();
        if (File.Exists(ht mlFilename))
        {
        _Logger.InfoFor mat("Converted file is {0}", htmlFilename);
        return htmlFilename;
        }
        else
        {
        _Logger.ErrorFo rmat("Converted file {0} does not exist", htmlFilename);
        return string.Empty;
        }
        }


        JScript code
        ============

        var oArgs = WScript.Argumen ts;

        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.DOMDOCUM ENT.6.0");
        var xml = new ActiveXObject(" MSXML2.DOMDocum ent.6.0");

        xml.async = false
        xml.resolveExte rnals = true

        xsl.async = false
        xsl.resolveExte rnals = true
        xsl.setProperty ("AllowDocument Function", true)
        xsl.setProperty ("AllowXsltScri pt", true)

        xml.validateOnP arse = false;
        xml.async = false;
        xml.load(xmlFil e);

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

        xsl.async = false;
        xsl.load(xslFil e);

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

        try
        {
        var result = xml.transformNo de(xsl.document Element)
        var fso = new ActiveXObject(" Scripting.FileS ystemObject");
        var a = fso.CreateTextF ile(htmlFile, true);
        a.WriteLine(res ult);
        a.Close();

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

        Comment

        Working...