How to read XML file on localhost from Silverlight application?

I have Vista strong> with IIS7 .

I want to create a simple Silverlight application that reads an XML file from localhost .

I created this file (which I had to copy and click "allow" as administrator):

C:\inetpub\wwwroot\data\customers.xml

and can see it when I go to the browser:

http://localhost/data/customers.xml

But when I run the following code, I get a target call exception :

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

So I created : C:\inetpub\wwwroot\crossdomainpolicy.xml

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
    <cross-domain-access>
        <policy >
            <allow-from http-request-headers="Content-Type">
                <domain uri="*"/>
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>

But I still get the target call exception exception .

Here is the complete internal exception:

{System.Security.SecurityException --- > System.Security.SecurityException: Sicherheitsfehler bei System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) bei System.Net.Browser.BrowserHttpWebRequest <. > C__DisplayClass5.b__4 ( sendState) bei System.Net.Browser.AsyncHelper. < > c__DisplayClass2.b__0 (Object sendState) --- Ende der internen Ausnahmestapelüberwachung --- bei System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, ) bei System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) bei System.Net.WebClient.GetWebResponse(WebRequest , IAsyncResult) bei System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult )}

update 1: Windows C:\inetpub\wwwroot\data IIS_USERS . .: - (

update 2: "" C:\inetpub\wwwroot\data, .: - (

update 3: : netsh http add urlacl url = http://+:80/ user = MYDOMAIN\MyUserName

, localhost Silverlight?

PRAGMATIC ANSWER:

- localhost, , live:

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

            }
        }        



    }
}
+3
5

, , , -, Silverlight -, Silverlight -.

+1
+1

http-request-headers="Content-Type", , .

0

, IIS Silverlight. HTTP-, , . Silverlight crossdomainpolicy.xml, IIS , ACL, - Silverlight .

After trying the infinite parameters in the crossdomainpolicy file, I decided to check out an alternative version of the Flash policy file crossdomain.xml, which Silverlight also understands, and it worked for the first time.

You can work for you.

<?xml version=""1.0""?>
<!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
<cross-domain-policy>
    <allow-http-request-headers-from domain=""*"" headers=""*""/>
</cross-domain-policy>

Change To try this, you will need to delete the crossdomainpolicy.xml file as it first requests Silverlight, and if it finds it, it will not request crossdomain.xml.

0
source

Your xml is unformatted and has extra> near the top.

0
source

Source: https://habr.com/ru/post/1705558/


All Articles