如何绑定到XmlDataProvider,其源具有默认命名空间(xmlns)设置?

时间:2009-12-14 07:28:11

标签: c# wpf xml xaml xpath

我的XML类似于以下

<?xml version="1.0" encoding="utf-8"?>
<foo name="FooBar" xmlns="http://mydomain/myapp/ver/myschema.xsd">
    <bars v="test">
        <bar bat="one"/>
        <bar bat="two"/>
        <bar bat="three"/>
    </bars>
</foo>

如何在WPF中映射它,如果我没有设置默认命名空间,它就可以工作,但是当我xmlns设置我的绑定时,它不匹配任何东西。我尝试了以下声明,但是我无法弄清楚如何映射组合框,如下所示。

<Window.Resources>
    <!-- works if xmlns is not set -->
    <XmlDataProvider x:Key="mySource1"
                     XPath="/foo">
    <!-- also tried -->
    <XmlDataProvider x:Key="mySource2"
                     XPath="//*[local-name()='foo']">
    <!-- also tried -->
    <XmlDataProvider x:Key="mySource3"
                     XPath="/foo">
        <XmlDataProvider.XmlNamespaceManager>
            <XmlNamespaceMappingCollection>
                <XmlNamespaceMapping
                    Uri="http://mydomain/myapp/ver/myschema.xsd"
                    Prefix=""/>
            </XmlNamespaceMappingCollection>
        </XmlDataProvider.XmlNamespaceManager>
    </XmlDataProvider>
</Window.Resources>

<StackPanel DataContext="{StaticResource mySource1}">
    <Label Content="{Binding XPath=@name}"/>
    <Label DataContext="{Binding XPath=bars}"
           Content="{Binding XPath=@v}"/>
</StackPanel>

我通过以下代码分配XmlDataProvider.Source属性。

XmlDataProvider xdp = FindResource("mySource1") as XmlDataProvider;
// ... setup dialog and confirm resource.
using (Stream s = dlg.OpenFile()) {
    XmlDocument doc = new XmlDocument();
    doc.Load(s);
    xdp.Document = doc;
}

非常感谢任何解决此问题的帮助,谢谢。

更新

根据安德鲁斯的建议,我想出了以下内容:

<Window.Resources>
    <XmlDataProvider x:Key="mySource"
                     XPath="/fb:foo">
        <XmlDataProvider.XmlNamespaceManager>
            <XmlNamespaceMappingCollection>
                <XmlNamespaceMapping
                    Uri="http://mydomain/myapp/ver/myschema.xsd"
                    Prefix="fb"/>
            </XmlNamespaceMappingCollection>
        </XmlDataProvider.XmlNamespaceManager>
    </XmlDataProvider>
</Window.Resources>

<StackPanel DataContext="{StaticResource mySource}">
    <Label Content="{Binding XPath=@name}"/>
    <ComboBox ItemsSource="{Binding XPath=fb:bars/fb:bar}"
              IsSynchronizedWithCurrentItem="True">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=@bat}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</StackPanel>

1 个答案:

答案 0 :(得分:1)

你能试试吗?我对xml名称空间不太熟悉。

<Window.Resources>
    <!-- also tried -->
    <XmlDataProvider x:Key="mySource3"
                     XPath="/cs:foo">
        <XmlDataProvider.XmlNamespaceManager>
            <XmlNamespaceMappingCollection>
                <XmlNamespaceMapping
                    Uri="http://mydomain/myapp/ver/myschema.xsd"
                    Prefix="cs"/>
            </XmlNamespaceMappingCollection>
        </XmlDataProvider.XmlNamespaceManager>
    </XmlDataProvider>
</Window.Resources>

<StackPanel DataContext="{StaticResource mySource1}">
    <Label Content="{Binding XPath=@name}"/>
    <Label Content="{Binding XPath=cs:bat}"/>
</StackPanel>