简体   繁体   中英

using linq to xml in silverlight?

I did implement a web application(asp.net,c#) where it has couple of pages .Each page has the ability to read the values from an xml file and passes into couple of text boxes in each page.User has the ability to edit the values and save,which inturn saves the xml file.I did use the linq to xml .

I have to move to the silverlight now.so i am trying to implement the same logic in a new silverlight project.I m trying to use the linq to xml in silverlight also.But some how i am unable to read the xml file which is not in the xap file.Here is my code

 XDocument doc = Document.Load("C:\Data\Data.exe.config");
            var applicationSettings = (from x in doc.Descendants("applicationSettings")
                                       from kvpair in .Element("Data.Properties.Settings").Elements("setting")
                                       select new
                                       {
                                           Name = kvpair.Attribute("name").Value,
                                           Node = kvpair.Element("value")
                                       }).ToDictionary(x => x.Name, y => y);

            string Account  = applicationSettings["Account no"].Node.Value.ToString();
           txtAccountno.Text = AttendanceWindow;
            string Details=applicationSettings["Details"].Node.Value.ToString();
            txtDetails.Text = Details;

I am assuming that "C:\\Data\\Data.exe.config" is a file that sits on the server? Keep in mind that Silverlight is executing on the client-side. Not only would you have to give Silverlight permission to access that directory (see this link for some details) , but you'd be accessing the client's hard disk, not the server's. In order to get that file's contents, you'd be better off parsing the XML server-side and sending whatever settings you require from it to Silverlight via web services.

If the file really does sit on the client's computer, then you need to create an out-of-browser Silverlight project: http://msdn.microsoft.com/en-us/library/ee721082(VS.95).aspx

EDIT:

Ah, I think I see what's going on now. The Document.Load method by default assumes the URI points to a resource within the XAP: http://msdn.microsoft.com/en-us/library/bb538371(v=vs.95).aspx

What you'll probably need to do is described in this MSDN article, which will use a stream approach to loading the XML: http://msdn.microsoft.com/en-us/library/cc645034(v=vs.95).aspx#Y0

Your problem is related to which areas on the disk you are allowed to access.

You need ensure that Silverlight can access the file, that it has rights to the files location, and that silverlight is configured to be able to access local files. Either the file must be moved to local storage or you must run with elevated privilages out of the browser.

See also: http://www.codeproject.com/KB/silverlight/FileExplorerInSilverlight.aspx for an example of accessing files from Silverlight

In Silverlight 5 it is possible to run the Silverlight application in-browser with elevated permissions, enabling you to access the entire file system. Otherwise you are restricted to the isolated storage area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM