Having difficulty exposing XmlDocument to JScript using ClearScript

210 Views Asked by At

Context: Windows Azure; Visual Studio Community 2015; ClearScript; JScript

I'm having problems exposing an XmlDocument object to the JScripts I'm executing using ClearScript.

I am instantiating a JScript interpreter using

JScriptEngine JSengine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableJITDebugging);

To instantiate the XmlDocument object I have tried both

using System.Xml;
...
JSengine.AddHostType("CSXmlDocument", typeof(XmlDocument));
...

and

...
JSengine.AddCOMObject("CSXmlDocument", "MSXML2.DOMDocument");
...

and I execute the JScript script using this

...
object answer = JSengine.Evaluate(File.ReadAllText(rulesetFilename));
...

My difficulty is in how to use the CSXmlDocument object inside the script. I have tried all of the following to no avail.

var xmlObj1 = CSXmlDocument;
var xmlObj2 = new CSXmlDocument;
var xmlObj3 = CSXmlDocument();
var xmlObj4 = new CSXmlDocument();

When CSXmlDocument is defined using AddCOMObject, the second through fourth invocations give an error of

Unable to evaluate the expression. Operation not supported. Unknown error: 0x8013baff.

The first invocation only gives me access to three methods, Equals, GetHashCode, GetType.

When CSXmlDocument is defined using AddHostType, the first gives just the three methods as above. The second invocation seems to give access to the full set of properties and methods (at least that's what VS2015 says in a debugging session) however, when I try to use the LoadXml method I get

xmlObj2.LoadXml(body)
Unable to evaluate the expression. Operation not supported. Unknown error: 0x8013baff.

I am currently working through two possible gotchas:

  1. Character encoding, in the light of another StackOverflow posting.
  2. HTML-XML markup conflicts.
2

There are 2 best solutions below

2
On

Instead of passing in the object, I elected to create the object in the script. Thus I have

var htmlDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");

What benefit there is in using FreeThreaded I don't know but it's working.

I still had problems, mind you, with how Msxml2 was handling the HTML I was throwing at it, and had to add the following

htmlDoc.setProperty("ProhibitDTD", false);
htmlDoc.validateOnParse = false;

Also Msxml2 can't handle   so I swapped that for its character equivalent:

body = body.replace(/ /g, " ");

The ProhibitDTD meant that ripping out the DOCTYPE was also required, viz

body = body.replace("<!DOCTYPE html>", "");

With all that done, the rest happened fairly quickly:

htmlDoc.loadXML(body);
leadLink = htmlDoc.selectNodes('//*[@title="View this lead"]')[0].attributes[0].value;
0
On

Hmm, I'm not sure what you're doing differently, but the following works for me:

using (var JSengine = new JScriptEngine())
{
    JSengine.AddHostType("CSXmlDocument", typeof(XmlDocument));
    JSengine.AddHostType("CSConsole", typeof(Console));
    JSengine.Execute(@"
        var doc = new CSXmlDocument();
        doc.LoadXml('<Hello>World</Hello>');
        var node = doc.FirstChild;
        CSConsole.WriteLine('{0} {1}!', node.Name, node.InnerText);");
}

This produces the expected output "Hello World!" with ClearScript 5.4.4. Does it work for you?