【发布时间】:2015-11-20 15:25:29
【问题描述】:
我有一个如下所示的 XML 文档:
<Window x:Name="winName" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="My Window" SizeToContent="WidthAndHeight">
<Grid ShowGridLines="true">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<GroupBox x:Name="grBox" Header="Main Group Box" Grid.Row="0" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="1" />
<TabControl x:Name="tabControl" Grid.Row="1" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="1">
<TabItem x:Name="mainTab" Header="Main Tab" />
</TabControl>
</Grid>
</Window>
我希望我的代码找到带有 x:Name mainTab 的 XElement TabItem。这就是我的代码的样子:
XDocument doc = XDocument.Load(path);
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xaml = "http://schemas.microsoft.com/winfx/2006/xaml";
IEnumerable<XElement> result = from myElems in doc.Descendants(xmlns + "Window")
where myElems.Attribute(xaml + "Name").Value == "mainTab"
select myElems;
但这不起作用,没有结果。请指教。
【问题讨论】:
标签: c# xml xaml linq-to-xml xelement