473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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<?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....

}

Cheers!
Jan 14 '10 #1
3 3610
JamieHowarth0
533 Expert 512MB
Hi markpittsnh,

Try using this:

Expand|Select|Wrap|Line Numbers
  1. using System.Xml;
  2.  
  3. XmlDocument doc = new XmlDocument();
  4. doc.Load(string yourDocumentUrl);
  5.  
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(string xmlContent).

Hope this helps,

codegecko
Jan 14 '10 #2
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 internetexplorer 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!
Jan 15 '10 #3
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);
}
Jan 21 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alexandre Fayolle | last post by:
Hi there, I'm not an emacs power user at all, just getting started with pymacs to try to get BicycleRepairMan to run. I have pymacs 0.21 installed, and on init, I get an emacs error. The...
3
by: Torrent | last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown...
6
by: JJBean | last post by:
Hi Oleg and All, Is this correct? Can I do this? <code> XmlDocument doc=new XmlDocument(); XPathNavigator nav = doc.CreateNavigator(); //Load the xml file. Change path if needed...
0
by: marcum williams | last post by:
Below I'm generating an updategram via xslt and currently referencing schema via <updg:sync mapping-schema="hpXSD.xsd"> .... </updg:sync> But I would like to LOAD the .xsd schema into memory...
0
by: Frank 'Olorin' Rizzi | last post by:
Hello everyone. This is quite convoluted, but I'll try to make it simple. I have a couple of bottom-line questions (I guess): 1~ what happens between the Page_Load routine in the code behind...
6
by: sylcheung | last post by:
Hi, How can I be notified when the document load is complet in JavaScript? I am referring to the whold document load is complete, mean all images/external files/frame/iframes have been loaded. ...
1
by: dbagirltx | last post by:
We have done some testing with mixed and forgotten results. So I'm hoping that asking here can clarify some issues for us. Right now we do one weekly warm backup. Throughout the week there are...
10
by: Chris Tomlinson | last post by:
Hi all, just a quickie. I hope someone has the answer. We have about 30 images on a page, and want to apply something like this to all of them: < ... oncontextmenu="alert('Message...
4
by: andrewcw | last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file and perform the transform. The MSDN documentation looks like it should be easy. But I get a compile error. Ideas ?...
1
by: Tomas | last post by:
When I try to load my xslt i get an xml exception with the message "Root element is missing". The stylesheet works when I preview it in stylus studio, but apparently not in my application. Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.