我有这个数据集,我正在使用Web服务。它看起来像是从Microsoft ADO生成的XML。我想将这些数据绑定到C#中的WPF数据网格,但我无法弄清楚如何编写命名空间。
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<rs:data>
<z:row ResumeID='7217937' FullName='Brianna Burgwell' AddressLine1='430 Bar Ranch Rd.' AddressLine2='' City='Buford' PostalCode='90210' State='CA' Country='US'/>
<z:row ResumeID='7218085' FullName='Mandy Philips' AddressLine1='200 Beltway Loop' AddressLine2='' City='Buford' PostalCode='90210' State='CA' Country='US'/>
</rs:data>
</xml>
如果我删除所有名称空间前缀并使其成为简单的XML,则此XAML可以工作:
<Grid.Resources>
<XmlDataProvider x:Key="applicants" Source="sample.xml" />
</Grid.Resources>
<DataGrid x:Name="applicantGrid" DataContext="{StaticResource applicants}" ItemsSource="{Binding XPath=/data/row}" AutoGenerateColumns="False" Margin="12,12,31,12" SelectionChanged="applicantGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="ResumeID" Binding="{Binding XPath=@ResumeID}" />
<DataGridTextColumn Header="Name" Binding="{Binding XPath=@FullName}" />
</DataGrid.Columns>
</DataGrid>
但是使用命名空间,我无法弄清楚XmlNamespaceMappingCollection的正确配置。
答案 0 :(得分:2)
您可以在XmlNamespaceMappingCollection
中设置名称空间集合,并使用XmlDataProvider
属性
XmlNamespaceManager
<Grid.Resources>
<XmlNamespaceMappingCollection x:Key="myNamespaces">
<XmlNamespaceMapping Uri="http://www.somenamespace.com" Prefix="namespace1"/>
<XmlNamespaceMapping Uri="http://www.anothernamespace.com" Prefix="namespace2"/>
</XmlNamespaceMappingCollection>
<XmlDataProvider x:Key="applicants" XmlNamespaceManager="{StaticResource myNamespaces}" Source="sample.xml" />
</Grid.Resources>
<DataGrid x:Name="applicantGrid" DataContext="{StaticResource applicants}" ItemsSource="{Binding XPath=/data/row}" AutoGenerateColumns="False" Margin="12,12,31,12" SelectionChanged="applicantGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="ResumeID" Binding="{Binding XPath=namespace1:ResumeID}" />
<DataGridTextColumn Header="Name" Binding="{Binding XPath=namespace2:FullName}" />
</DataGrid.Columns>
</DataGrid>