title: How to read an XML file on localhost from Silverlight application?
body: <p>I have <strong>Vista</strong> with <strong>IIS7</strong>.</p>
  
  <p>I want to create a simple Silverlight application that <strong>reads an xml file from localhost</strong>.</p>
  
  <p>I created this file (which I had to copy and click "allow" as administrator): </p>
  
  <blockquote>
    <p><strong><code>C:\inetpub\wwwroot\data\customers.xml</code></strong></p>
  </blockquote>
  
  <p>and can see it when I go here in a browser: </p>
  
  <blockquote>
    <p><strong><code>http://localhost/data/customers.xml</code></strong></p>
  </blockquote>
  
  <p>But when I run the following code, I get a <strong>target invocation exception</strong>:</p>
  
  <pre><code>using System;
  using System.Net;
  using System.Windows.Controls;
  using System.IO;
  
  namespace TestXmlRead234
  {
      public partial class MainPage : UserControl
      {
          public MainPage()
          {
              InitializeComponent();
  
              WebClient client = new WebClient();
              client.OpenReadAsync(new Uri("http://localhost/data/customers.xml", UriKind.Absolute));
              client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
          }
  
          void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
          {
              StreamReader myReader = new StreamReader(e.Result);
              Output.Text = myReader.ReadLine();
              myReader.Close();
          }
      }
  }
  </code></pre>
  
  <p>So I created <strong><code>C:\inetpub\wwwroot\crossdomainpolicy.xml</code></strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;access-policy&gt;
      &lt;cross-domain-access&gt;
          &lt;policy &gt;
              &lt;allow-from http-request-headers="Content-Type"&gt;
                  &lt;domain uri="*"/&gt;
              &lt;/allow-from&gt;
              &lt;grant-to&gt;
                  &lt;resource path="/" include-subpaths="true"/&gt;
              &lt;/grant-to&gt;
          &lt;/policy&gt;
      &lt;/cross-domain-access&gt;
  &lt;/access-policy&gt;
  </code></pre>
  
  <p>But I still get the <strong>target invocation exception</strong> error. </p>
  
  <p>Here is the full inner exception:</p>
  
  <blockquote>
    <p>{System.Security.SecurityException
    ---> System.Security.SecurityException:
    Sicherheitsfehler    bei
    System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult
    asyncResult)    bei
    System.Net.Browser.BrowserHttpWebRequest.&lt;>c__DisplayClass5.b__4(Object
    sendState)    bei
    System.Net.Browser.AsyncHelper.&lt;>c__DisplayClass2.b__0(Object sendState)    --- Ende der internen
    Ausnahmestapelüberwachung ---    bei
    System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback
    beginMethod, Object state)    bei
    System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult
    asyncResult)    bei
    System.Net.WebClient.GetWebResponse(WebRequest
    request, IAsyncResult result)    bei
    System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult
    result)}</p>
  </blockquote>
  
  <p><strong>update 1:</strong> In windows explorer, I then right clicked <strong><code>C:\inetpub\wwwroot\data</code></strong> and made IIS_USERS a co-owner of that directory. But still get the same error. :-(</p>
  
  <p><strong>update 2:</strong> also made "everyone" co-owner of  <strong><code>C:\inetpub\wwwroot\data</code></strong>, same error. :-(</p>
  
  <p><strong>update 3:</strong> opened command window as administrator and executed this command: netsh http add urlacl url=<a href="http://+:80/" rel="nofollow noreferrer">http://+:80/</a> user=MYDOMAIN\MyUserName</p>
  
  <p><strong>What else do I have to be able to read a text file from localhost from a Silverlight application?</strong></p>
  
  <p><H1>PRAGMATIC ANSWER:</H1></p>
  
  <p>For testing locally just publish to the temporary localhost webserver port for which you don't even need a cross-domain file, then make necessary changes when you publish live:</p>
  
  <pre><code>using System;
  using System.Linq;
  using System.Net;
  using System.Windows.Controls;
  using System.IO;
  using System.Xml.Linq;
  
  namespace TestWeb124
  {
      public partial class MainPage : UserControl
      {
          public MainPage()
          {
              InitializeComponent();
  
              WebClient wc = new WebClient();
              wc.OpenReadAsync(new Uri("http://localhost:49512/customers.xml", UriKind.Absolute));
              wc.OpenReadCompleted += wc_OpenReadCompleted;
          }
  
          private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
          {
              if (e.Error != null)
              {
                  Output.Text = e.Error.Message;
                  return;
              }
              using (Stream s = e.Result)
              {
                  XDocument doc = XDocument.Load(s);
                  Output.Text = doc.ToString(SaveOptions.OmitDuplicateNamespaces);
                  var customers = from c in doc.Descendants("customer")
                                  select new
                                  {
                                      FirstName = c.Element("firstName").Value
                                  };
  
                  foreach (var customer in customers)
                  {
                      Output.Text += customer.FirstName;
                  }
  
              }
          }        
  
  
  
      }
  }
  </code></pre>