使用 XDocument 读取 xaml 文件

发布于 2024-11-17 14:23:50 字数 1073 浏览 0 评论 0原文

我有以下 silverlight 示例数据 xaml 文件,它在设计模式下完美运行:

<viewmodel:MapViewModel 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodel="clr-namespace:MyApplication.ViewModels">
<viewmodel:MapViewModel.Lines>
    <viewmodel:Line Line="M 100 100 L 280 100 S 302 102 315 115 L 500 300"/>
</viewmodel:MapViewModel.Lines></viewmodel:MapViewModel>

由于此数据在运行时也很有用(可能不会永远),因此我想在后面的代码中读取 xml。因此我使用 XDocument 并且它有效,意味着我可以阅读和解析整个文档。但我不知道如何访问这些元素。我该如何访问“线路”?

我尝试过的是:

            var lines = doc.Descendants(XName.Get("Line", "viewmodel:")).ToArray();

它总是不返回任何内容。因为我认为这是命名空间的问题,所以我尝试了

  • “viewmodel”“
  • viewmodel:”
  • “MyApplication.ViewModels”
  • “MyApplication.ViewModels:”
  • “clr-namespace:MyApplication.ViewModels”和
  • “clr-namespace:MyApplication。 ViewModels:“

有人能告诉我我做错了什么吗?缺少 xml-header 是原因吗?我读取了一个带有正确标头(但没有命名空间)的 xml 文件,并且它有效。

I have the following silverlight sample data xaml file, which works perfect in design mode:

<viewmodel:MapViewModel 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodel="clr-namespace:MyApplication.ViewModels">
<viewmodel:MapViewModel.Lines>
    <viewmodel:Line Line="M 100 100 L 280 100 S 302 102 315 115 L 500 300"/>
</viewmodel:MapViewModel.Lines></viewmodel:MapViewModel>

Since this data is also useful during runtime (may be not for ever), I want to read the xml within code behind. Therefor I use XDocument and it works, means I can read and parse the whole document. But I cannot figure out, how to access the elements. Hoe do I acces a "Line"?

What I've tried is:

            var lines = doc.Descendants(XName.Get("Line", "viewmodel:")).ToArray();

It allways returns nothing. Since I assumed it is a problem of the namespace, I've tried

  • "viewmodel"
  • "viewmodel:"
  • "MyApplication.ViewModels"
  • "MyApplication.ViewModels:"
  • "clr-namespace:MyApplication.ViewModels" and
  • "clr-namespace:MyApplication.ViewModels:"

Can someone tell me what I'm making wrong? Is the missing xml-header the reason? I read an xml-file with a correct header (but without a namespace) allready and it worked.

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

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

发布评论

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

评论(1

碍人泪离人颜 2024-11-24 14:23:50

使用 XNamespace:

var xdoc = XDocument.Parse(xaml);
XNamespace ns = "clr-namespace:MyApplication.ViewModels";
var lines = xdoc.Descendants(ns + "Line").ToArray();

您还可以使用以下语法:

var lines = xdoc.Descendants("{clr-namespace:MyApplication.ViewModels}Line").ToArray();

use an XNamespace:

var xdoc = XDocument.Parse(xaml);
XNamespace ns = "clr-namespace:MyApplication.ViewModels";
var lines = xdoc.Descendants(ns + "Line").ToArray();

you can also use this syntax:

var lines = xdoc.Descendants("{clr-namespace:MyApplication.ViewModels}Line").ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文