0

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.
Community
  • 1
  • 1
bugmagnet
  • 7,631
  • 8
  • 69
  • 131

2 Answers2

1

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?

BitCortex
  • 3,328
  • 1
  • 15
  • 19
0

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 &nbsp; so I swapped that for its character equivalent:

body = body.replace(/&nbsp;/g, "&#160;");

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;
bugmagnet
  • 7,631
  • 8
  • 69
  • 131
  • I'm not an XML expert, but `XmlDocument` also rejects the `nbsp` entity when given plain text, but it throws an exception with a helpful message, explaining that the entity is undefined. In any case, I'm not sure why you'd want to use `ActiveXObject`/MSXML in this environment; the .NET class seems easier to use and works with other script engines. – BitCortex Jan 22 '16 at 12:23
  • True, and I may yet go back to it, once I figure out how to make it work. In the meantime, I have a working tool. – bugmagnet Jan 22 '16 at 14:25