I have a use-case where I need to bind data from an XML file to a WPF DataGrid. I prepared this example to demonstrate what I'll be doing in my final code.
我有一个用例,我需要将XML文件中的数据绑定到WPF DataGrid。我准备这个例子来演示我将在最终代码中做什么。
This is Books.xml:
这是Books.xml:
<?xml version="1.0" encoding="utf-8" ?>
<library>
<books>
<book id="1" name="The First Book" author="First Author">
First Book Content
</book>
<book id="2" name="The Second Book" author="Second Author">
Second Book Content
</book>
</books>
</library>
And here is how I bind it my DataGrid control. First XAML:
这是我如何绑定我的DataGrid控件。第一个XAML:
<Window x:Class="LinqToXmlBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="300" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="268*" />
<ColumnDefinition Width="110*" />
</Grid.ColumnDefinitions>
<toolkit:DataGrid Name="xmlBoundDataGrid" Margin="1" ItemsSource="{Binding Path=Elements[book]}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Book ID" Binding="{Binding Path=Attribute[id].Value}"/>
<toolkit:DataGridTextColumn Header="Book Name" Binding="{Binding Path=Attribute[name].Value}"/>
<toolkit:DataGridTextColumn Header="Content" Binding="{Binding Path=Value}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
<StackPanel Name="myStackPanel" Grid.Column="1">
<Button Name="bindToXmlButton" Click="bindToXmlButton_Click">Bind To XML</Button>
</StackPanel>
</Grid>
</Window>
Then, C# code:
然后,C#代码:
const string _xmlFilePath = "..//..//Books.xml";
private void bindToXmlButton_Click(object sender, RoutedEventArgs e)
{
XElement books = XElement.Load(_xmlFilePath).Element(myNameSpace + "books");
xmlBoundDataGrid.DataContext = books;
}
Now, if I defined an XML namespace at the root element of Books.XML to be http://my.namespace.com/books
; I know I can get that namespace programmatically like so:
现在,如果我将Books.XML的根元素的XML命名空间定义为http://my.namespace.com/books;我知道我可以通过编程方式获得该命名空间:
XNamespace myNameSpace = XElement.Load(_xmlFilePath).Attribute("xmlns").Value;
But, how can I retrieve this namespace in XAML in order to access the "book" element? And what are the best practices in that regard?
但是,如何在XAML中检索此命名空间以访问“book”元素?那方面的最佳做法是什么?
Thank you very much.
非常感谢你。
1 个解决方案
#1
0
Sorry if I got you wrong, but
对不起,如果我弄错了,但是
-
If you need to access elements from default namespace like xmlns="...", you should use regular syntax like Path=Attribute[name].Value
如果需要访问默认命名空间中的元素,如xmlns =“...”,则应使用常规语法,如Path = Attribute [name] .Value
-
If you have XML with namespace with prefix, like xmlns:ns="..." and elements inside this namespace like , you may try to use Path=Elements["ns:book"]
如果你的XML带有带前缀的命名空间,比如xmlns:ns =“...”和这个命名空间里面的元素,你可以尝试使用Path = Elements [“ns:book”]
Hope this helps.
希望这可以帮助。
#1
0
Sorry if I got you wrong, but
对不起,如果我弄错了,但是
-
If you need to access elements from default namespace like xmlns="...", you should use regular syntax like Path=Attribute[name].Value
如果需要访问默认命名空间中的元素,如xmlns =“...”,则应使用常规语法,如Path = Attribute [name] .Value
-
If you have XML with namespace with prefix, like xmlns:ns="..." and elements inside this namespace like , you may try to use Path=Elements["ns:book"]
如果你的XML带有带前缀的命名空间,比如xmlns:ns =“...”和这个命名空间里面的元素,你可以尝试使用Path = Elements [“ns:book”]
Hope this helps.
希望这可以帮助。