2

I have this dataset I'm consuming from a web service. It looks like XML generated from Microsoft ADO. I'd like to bind this data to a WPF datagrid in C# but I can't quite figure out how to code the namespaces.

<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>

If I strip out all of the namespace prefixes and make it simple XML, this XAML works:

<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>

But with the namespaces, I can't figure out the right configuration of XmlNamespaceMappingCollection.

1 Answer 1

2

You can set a collection of namespaces in the XmlNamespaceMappingCollection and apply to your XmlDataProvider using the XmlNamespaceManager property

<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.