使用 XDocument 读取 xaml 文件
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 XNamespace:
您还可以使用以下语法:
use an XNamespace:
you can also use this syntax: