将用户配置数据保存到 XML 和相对 URI 错误

发布于 2024-08-24 16:12:00 字数 771 浏览 2 评论 0原文

晚上好,

我正在开发一个程序,其中一些应用程序配置信息存储在 Userconfig.xml 文件中。我通过相对 URI 将文件作为 XAML 中的 XMLDataProvider 加载:

<XmlDataProvider x:Name="UserConfigDataSource" x:Key="UserConfigDataSource" Source="UserConfig.xml" d:IsDataSource="True"/>

我有许多项目始终绑定到文档中的元素以及保存到 XMLDataProvider 的事件处理程序:

Private Sub SaveConfig(ByVal sender as Object, ByVal e as System.EventArgs)
    'TODO: Add event handler implementation here

    Dim SavePath As String = UserConfigDataSource.Source.LocalPath.ToString
    Dim XMLDoc = UserConfigDataSource.Document
    UserConfigDataSource.Document.Save(SavePath)
End Sub

当执行此操作时,我收到错误“此操作不是支持相对 URI”。有没有一种好方法来生成绝对 URI(除了获取程序集执行位置并从末尾修剪可执行文件名之外)?我预计这是一个稍微简单的过程。任何帮助将不胜感激。

科里

Good evening,

I am working on a program where some application config info is stored in a Userconfig.xml file. I am loading the file as an XMLDataProvider in the XAML via relative URI:

<XmlDataProvider x:Name="UserConfigDataSource" x:Key="UserConfigDataSource" Source="UserConfig.xml" d:IsDataSource="True"/>

I have a number of items throughout bound to elements in the document and an event handler that saves to the XMLDataProvider:

Private Sub SaveConfig(ByVal sender as Object, ByVal e as System.EventArgs)
    'TODO: Add event handler implementation here

    Dim SavePath As String = UserConfigDataSource.Source.LocalPath.ToString
    Dim XMLDoc = UserConfigDataSource.Document
    UserConfigDataSource.Document.Save(SavePath)
End Sub

When this executes I get the error "This operation is not supported for a relative URI". Is there a good way to produce an absolute URI (aside from getting the assembly executing location and trimming the executable filename from the end)? I expected this to be a somewhat simple procedure. Any help would be greatly appreciated.

Cory

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

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

发布评论

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

评论(2

内心荒芜 2024-08-31 16:12:00

您是否查看过调试器中 SavePath 的确切值?由于它是 XAML 中的 URI,因此 XmlDocument.Save 可能无法理解它的语法。
您也可以尝试 AbsoluteUri.ToString

Have you looked at the exact value of SavePath in the debugger ? As it was a URI in XAML, it may have a syntax not understood by XmlDocument.Save.
You could also try AbsoluteUri.ToString

瑾夏年华 2024-08-31 16:12:00

使用了此处的建议,

FileInfo config= new FileInfo("configuration.xml"); 
provider.Source = new System.Uri(config.FullName);

一切顺利。

科里

Used the suggestion HERE

FileInfo config= new FileInfo("configuration.xml"); 
provider.Source = new System.Uri(config.FullName);

All worked well.

Cory

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