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

2 に答える 2

1

うーん、あなたが何をしているのかはわかりませんが、私にとっては次のように機能します:

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);");
}

これにより、期待される出力 "Hello World!" が生成されます。ClearScript 5.4.4 で。それはあなたのために働きますか?

于 2016-01-22T04:55:59.240 に答える