Associating XML data with namespaces

I want to use data binding to an XML document to fill out a simple form that shows details about a list of people. Everything is set up for me and it works like this right now:

<Window x:Class="DataBindingSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
    <XmlDataProvider x:Key="xmlProvider" XPath="People" Source="c:\someuri.xml"/>
</Window.Resources>
<Grid>        
    <ListBox Name="personList" ItemsSource="{Binding Source={StaticResource xmlProvider}, XPath=Person}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <GroupBox Header="GroupBox" Name="groupBox1" DataContext="{Binding ElementName=personList, Path=SelectedItem}">
        <Grid>
            <TextBox Name="nameText" Text="{Binding XPath=Name}"/>
            <ComboBox Name="genderCombo" Text="{Binding XPath=Gender}">
                <ComboBoxItem>Male</ComboBoxItem>
                <ComboBoxItem>Female</ComboBoxItem>
            </ComboBox>
        </Grid>
    </GroupBox>
</Grid>
</Window>

(All position / layout elements have been removed for clarity)

Now it works great! If I provide it with some XML that matches the specified paths, I get a list of names in the list that shows the name and gender in the corresponding fields when clicked. The problem occurs when I start trying to use namespaces in my XML source. Then XAML will look like this:

<Window x:Class="DataBindingSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
    <XmlNamespaceMappingCollection x:Key="namespaceMappings">
        <XmlNamespaceMapping Uri="http://www.mynamespace.com" Prefix="mns"/>
    </XmlNamespaceMappingCollection>
    <XmlDataProvider x:Key="xmlProvider" XmlNamespaceManager="{StaticResource namespaceMappings}" XPath="mns:People" Source="c:\someuriwithnamespaces.xml"/>
</Window.Resources>
<Grid>        
    <ListBox Name="personList" ItemsSource="{Binding Source={StaticResource xmlProvider}, XPath=mns:Person}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=mns:Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

    <GroupBox Header="GroupBox" Name="groupBox1" DataContext="{Binding ElementName=personList, Path=SelectedItem}">
        <Grid>
            <TextBox Name="nameText" Text="{Binding XPath=mns:Name}"/>
            <ComboBox Name="genderCombo" Text="{Binding XPath=mns:Gender}">
                <ComboBoxItem>Male</ComboBoxItem>
                <ComboBoxItem>Female</ComboBoxItem>
            </ComboBox>
        </Grid>
    </GroupBox>
</Grid>
</Window>

(, , XML- ), Listbox - , Name Gender! , - xml groupbox DataContext, , . - , XML ?

+3
2

XPath :

 <TextBox Name="nameText">
    <TextBox.Text>
       <Binding XPath="*[local-name()='Name']" />
    </TextBox.Text>
 </TextBox>
+1

WPF MSDN. , , . , :

:

                                   

    <XmlDataProvider x:Key="dataProvider"
                     XmlNamespaceManager="{StaticResource namespaceMappings}"
                     XPath="p:players/p:player">
        <x:XData>
            <p:players xmlns:p="http://www.footballism.com/2005/SoccerPlayers">
                <p:player>
                    <p:fullName>Sebastian Batistuta</p:fullName>
                    <p:age>26</p:age>
                </p:player>
                <p:player>
                    <p:fullName>Andriey Shevchenko</p:fullName>
                    <p:age>30</p:age>
                </p:player>
                <p:player>
                    <p:fullName>Paviel Nedved</p:fullName>
                    <p:age>21</p:age>
                </p:player>
                <p:player>
                    <p:fullName>David Beckham</p:fullName>
                    <p:age>19</p:age>
                </p:player>
            </p:players>
        </x:XData>
    </XmlDataProvider>
</Page.Resources>
<StackPanel>
    <TextBlock
        Text="{Binding XPath=p:fullName}"
        FontWeight="Bold"
        Binding.XmlNamespaceManager="{StaticResource namespaceMappings}"
        DataContext="{Binding ElementName=listBox, Path=SelectedItem}"/>
    <ListBox ItemsSource="{Binding Source={StaticResource dataProvider}}"
             x:Name="listBox"
             DisplayMemberPath="p:fullName">
    </ListBox>
</StackPanel> </Page>

, , , Binding.XmlNamespaceManager TextBlock.

ListBox - , , xml , TextBlock , .

, - , :

                                   

    <XmlDataProvider x:Key="dataProvider"
                     XmlNamespaceManager="{StaticResource namespaceMappings}"
                     XPath="p:players/p:player">
        <x:XData>
            <p:players xmlns:p="http://www.footballism.com/2005/SoccerPlayers">
                <p:player>
                    <p:fullName>Sebastian Batistuta</p:fullName>
                    <p:age>26</p:age>
                </p:player>
                <p:player>
                    <p:fullName>Andriey Shevchenko</p:fullName>
                    <p:age>30</p:age>
                </p:player>
                <p:player>
                    <p:fullName>Paviel Nedved</p:fullName>
                    <p:age>21</p:age>
                </p:player>
                <p:player>
                    <p:fullName>David Beckham</p:fullName>
                    <p:age>19</p:age>
                </p:player>
            </p:players>
        </x:XData>
    </XmlDataProvider>
</Page.Resources>
<StackPanel DataContext="{Binding Source={StaticResource dataProvider}}">
    <TextBlock
        Text="{Binding XPath=p:fullName}"
        FontWeight="Bold"/>
    <ListBox ItemsSource="{Binding}"
             x:Name="listBox"
             DisplayMemberPath="p:fullName"
             IsSynchronizedWithCurrentItem="True">
    </ListBox>
</StackPanel> </Page>

, .

+2

Source: https://habr.com/ru/post/1698133/


All Articles