在silverlight中使用linq to xml?

发布于 2024-12-05 16:18:44 字数 1132 浏览 4 评论 0原文

我确实实现了一个 Web 应用程序(asp.net,c#),其中有几个页面。每个页面都能够从 xml 文件中读取值并将其传递到每个页面中的几个文本框中。用户能够编辑值并保存,这又保存了 xml 文件。我确实使用了 linq to xml 。

我现在必须转移到 silverlight。所以我尝试在新的 silverlight 项目中实现相同的逻辑。我也尝试在 silverlight 中使用 linq to xml。但是有些我无法读取 xml 文件,该文件是不在 xap 文件中。这是我的代码

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟织青萝梦 2024-12-12 16:18:44

我假设“C:\Data\Data.exe.config”是位于服务器上的文件?请记住,Silverlight 是在客户端执行的。您不仅需要授予 Silverlight 访问该目录的权限(有关详细信息,请参阅此链接),但您将访问客户端的硬盘,而不是服务器的硬盘。为了获取该文件的内容,您最好解析 XML 服务器端并通过 Web 服务将您需要的任何设置发送到 Silverlight。

如果该文件确实位于客户端计算机上,那么您需要创建一个浏览器外的 Silverlight 项目: http://msdn.microsoft.com/en-us/library/ee721082(VS.95).aspx

编辑:

啊,我想我明白现在发生了什么。默认情况下,Document.Load 方法假定 URI 指向 XAP 中的资源: http://msdn.microsoft.com/en-us/library/bb538371(v=vs.95).aspx

您可能需要执行的操作这篇 MSDN 文章中对此进行了描述,它将使用流方法来加载 XML: http://msdn.microsoft.com/en-us/library/cc645034(v=vs.95).aspx#Y0

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

森林迷了鹿 2024-12-12 16:18:44

您的问题与允许您访问磁盘上的哪些区域有关。

您需要确保 Silverlight 可以访问该文件,它具有文件位置的权限,并且 silverlight 配置为能够访问本地文件。文件必须移至本地存储,或者您必须在浏览器中以提升的权限运行。

另请参阅:http://www.codeproject.com/KB/silverlight/FileExplorerInSilverlight.aspx< /a> 从 Silverlight 访问文件的示例

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

清欢 2024-12-12 16:18:44

在 Silverlight 5 中,可以使用提升的权限在浏览器中运行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文