Home > Mobile >  WebView2 ExecuteScriptAsync with XML input
WebView2 ExecuteScriptAsync with XML input

Time:12-22

I want to run a C# function using ExecuteScriptAsync() with XML text as input.

Something like that:

var xml = "<?xml version=\"1.0\" encoding=\"UTF - 16\" standalone=\"no\" ?><values>42</values>";
webView2Control.CoreWebView2.Navigate("file:///C:/Users/erezf/AppData/Local/Temp/index.html");
var input = "func("   xml   ")";
await webView2Control.CoreWebView2.ExecuteScriptAsync(input);

The html file include the function func:

<script id="test" type="text/javascript">
    function func(xml) { alert(xml); }
</script>

This code doesn't work, why?

CodePudding user response:

Your XML string being passed to the Javascript function is missing single or double quotes. If you look at the input variable in the debugger, it will look like:

func(<?xml version="1.0" encoding="UTF - 16" standalone="no" ?><values>42</values>)

Simply add single quotes like this:

var xml = "'<?xml version=\"1.0\" encoding=\"UTF - 16\" standalone=\"no\" ?><values>42</values>'";

CodePudding user response:

In my case the solution was:

1. add ' ' in the " ".
2. replace the "\n" with "\\n".
  • Related