title: Show local XML document with WKWebView
body: <p>I'm trying to display a XML file with <code>WKWebView</code>. Has anyone got it managed to display it? I've set up a complete sample app (see below). The results were obtained by testing on iOS 9.3 iPhone 4S simulator.</p>
  
  <p><strong><code>LoadFileUrl()</code></strong></p>
  
  <pre><code>string targetFilepath = Init("simplexsl.xml");
  Init("simple.xsl");
  
  NSUrl url = new NSUrl("file:///" + targetFilepath, false);
  NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
  webview.LoadFileUrl(url, readAccessUrl);
  </code></pre>
  
  <p>Result: white page</p>
  
  <p><strong><code>LoadHtmlString()</code></strong></p>
  
  <pre><code>string targetFilepath = Init("simplexsl.xml");
  Init("simple.xsl");
  
  NSData documentData = NSData.FromFile(targetFilepath);
  
  if(documentData != null &amp;&amp; documentData.Length &gt; 0)
  {
      NSString htmlString = NSString.FromData(documentData, NSStringEncoding.UTF8);
      webview.LoadHtmlString(htmlString, readAccessUrl);
  }
  </code></pre>
  
  <p>Result: content of xml is shown (without tags from xml), but styling is not applied.</p>
  
  <pre><code>NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
  
  NSString htmlString = new NSString(File.ReadAllText(targetFilepath));
  webview.LoadHtmlString(htmlString, readAccessUrl);
  </code></pre>
  
  <p>Result: content of xml is shown (without tags from xml), but styling is not applied.</p>
  
  <p><strong><code>LoadData()</code></strong></p>
  
  <pre><code>string targetFilepath = Init("simplexsl.xml");
  Init("simple.xsl");
  
  NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
  
  NSData documentData = NSData.FromFile(targetFilepath);
  
  if(documentData != null &amp;&amp; documentData.Length &gt; 0)
  {
      webview.LoadData(documentData, "text/xml", "utf-8", readAccessUrl);
  }
  </code></pre>
  
  <p>Result: white page</p>
  
  <p><strong><code>LoadRequest()</code></strong></p>
  
  <pre><code>string targetFilepath = Init("simplexsl.xml");
  Init("simple.xsl");
  
  NSUrl url = new NSUrl("file:///" + targetFilepath, false);
  webview.LoadRequest(new NSUrlRequest(url));
  </code></pre>
  
  <p>Result: white page</p>
  
  <p><strong>Different paths</strong></p>
  
  <pre><code>var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User) [0].Path;
  var caches = Path.Combine (documents, "Caches");
  </code></pre>
  
  <p>Result: white page</p>
  
  <pre><code>var caches = Path.Combine(System.IO.Path.GetTempPath(), "www");
  Directory.CreateDirectory(caches);
  </code></pre>
  
  <p>Result: white page</p>
  
  <p><strong>Bundle Resource</strong></p>
  
  <pre><code>string folder = NSBundle.MainBundle.BundlePath;
  string source = Path.Combine(folder, "simplexsl.xml");
  webview.LoadFileUrl(new NSUrl(source,false), new NSUrl(folder, true))
  </code></pre>
  
  <p>Result: white page</p>
  
  <p><strong>Link</strong></p>
  
  <p>Now I loaded a html with a link in it</p>
  
  <pre><code>&lt;a href="simplexsl.xml"&gt;XML Link&lt;/a&gt;
  </code></pre>
  
  <p>Result: html page is loaded, clicking the link leads to a white page, if xsl is commented out in xml then the xml is shown without styling</p>
  
  <p><strong>Online</strong></p>
  
  <pre><code>webview.LoadRequest(new NSUrlRequest(new NSUrl("http://www.w3schools.com/xml/simplexsl.xml")));
  </code></pre>
  
  <p>Result: <code>WKWebView</code> can show the <a href="http://www.w3schools.com/xml/simplexsl.xml" rel="nofollow">XML file</a> if you allow it load non https resources.</p>
  
  <p><strong>Without XSL</strong></p>
  
  <p>Comment out the following line in <em>simplexsl.xml</em></p>
  
  <pre><code>&lt;?xml-stylesheet type="text/xsl" href="simple.xsl" ?&gt;
  </code></pre>
  
  <p>and using</p>
  
  <pre><code>webview.LoadFileUrl(url, readAccessUrl);
  </code></pre>
  
  <p>Result: content of xml is shown (without tags from xml), but styling is not applied. So it seems that there is a <code>basepath</code> issue, but only with XSL files?</p>
  
  <p>Displaying a html file, which references a picture relatively, does work, when I use <code>LoadFileUrl()</code>. But with xml files there seems to be a problem. For a short test <code>UIWebView</code> seems to handle that better.</p>
  
  <h1>So how is it done correctly?</h1>
  
  <hr>
  
  <p><strong>Basic setup</strong></p>
  
  <pre><code>public override void ViewDidLoad()
  {
      base.ViewDidLoad();
  
      var webview = new WKWebView(View.Bounds, new WKWebViewConfiguration());
      View.AddSubview(webview);
  
      // code here
  }
  
  private string Init(string filename)
  {
      string sourceFilepath = Path.Combine(NSBundle.MainBundle.BundlePath, filename);
      var library = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).Replace("Documents", "Library");
      var caches = Path.Combine(library, "Caches");
      var targetFilepath = Path.Combine(caches, filename);
  
      if (CopyFile(sourceFilepath, targetFilepath))
          return targetFilepath;
  
      return string.Empty;
  }
  
  private bool CopyFile(string sourceFilepath, string targetFilepath)
  {
      if (File.Exists(sourceFilepath))
      {
          File.Copy(sourceFilepath, targetFilepath, true);
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p><code>targetFilepath</code> is something like <code>/Users/some-user/Library/Developer/CoreSimulator/Devices/8808B80D-D232-4599-B776-139409C9DDB8/data/Containers/Data/Application/BC84CFD0-A805-417C-912F-C2B07834C822/Library/Caches/simplexsl.xml</code></p>
  
  <p><em>simplexsl.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?xml-stylesheet type="text/xsl" href="simple.xsl" ?&gt;
  &lt;breakfast_menu&gt;
    &lt;food&gt;
      &lt;name&gt;Belgian Waffles&lt;/name&gt;
      &lt;price&gt;$5.95&lt;/price&gt;
      &lt;description&gt;two of our famous Belgian Waffles with plenty of real maple syrup&lt;/description&gt;
      &lt;calories&gt;650&lt;/calories&gt;
    &lt;/food&gt;
    &lt;food&gt;
      &lt;name&gt;Strawberry Belgian Waffles&lt;/name&gt;
      &lt;price&gt;$7.95&lt;/price&gt;
      &lt;description&gt;light Belgian waffles covered with strawberries and whipped cream&lt;/description&gt;
      &lt;calories&gt;900&lt;/calories&gt;
    &lt;/food&gt;
    &lt;food&gt;
      &lt;name&gt;Berry-Berry Belgian Waffles&lt;/name&gt;
      &lt;price&gt;$8.95&lt;/price&gt;
      &lt;description&gt;light Belgian waffles covered with an assortment of fresh berries and whipped cream&lt;/description&gt;
      &lt;calories&gt;900&lt;/calories&gt;
    &lt;/food&gt;
    &lt;food&gt;
      &lt;name&gt;French Toast&lt;/name&gt;
      &lt;price&gt;$4.50&lt;/price&gt;
      &lt;description&gt;thick slices made from our homemade sourdough bread&lt;/description&gt;
      &lt;calories&gt;600&lt;/calories&gt;
    &lt;/food&gt;
    &lt;food&gt;
      &lt;name&gt;Homestyle Breakfast&lt;/name&gt;
      &lt;price&gt;$6.95&lt;/price&gt;
      &lt;description&gt;two eggs, bacon or sausage, toast, and our ever-popular hash browns&lt;/description&gt;
      &lt;calories&gt;950&lt;/calories&gt;
    &lt;/food&gt;
  &lt;/breakfast_menu&gt;
  </code></pre>
  
  <p><em>simple.xsl</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"&gt;
      &lt;xsl:for-each select="breakfast_menu/food"&gt;
        &lt;div style="background-color:teal;color:white;padding:4px"&gt;
          &lt;span style="font-weight:bold"&gt;
            &lt;xsl:value-of select="name"/&gt; -
          &lt;/span&gt;
          &lt;xsl:value-of select="price"/&gt;
        &lt;/div&gt;
        &lt;div style="margin-left:20px;margin-bottom:1em;font-size:10pt"&gt;
          &lt;p&gt;
            &lt;xsl:value-of select="description"/&gt;
            &lt;span style="font-style:italic"&gt;
              (&lt;xsl:value-of select="calories"/&gt; calories per serving)
            &lt;/span&gt;
          &lt;/p&gt;
        &lt;/div&gt;
      &lt;/xsl:for-each&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It would be no problem if you post your solutions in Objective-C or Swift.</p>