简体   繁体   中英

WPF XMLDataProvider do not work for union of XPath

The XMLDataProvider is not returning outcome for XPath query having union. Please see my problem statement at bottam after the code.

In WPF XMLDataProvider, I am using below somestrings.xml as below,

<?xml version="1.0" encoding="utf-8" ?>
<MyRoot>
<App1>
    <Common>
    <ApplicationName>Online Games</ApplicationName>
    </Common>
   <Screen1>
    <SelectGameshButtonName>Select Games</SelectGameshButtonName>
   </Screen1>  
   <Screen2>
    <FinishButtonName>Finish Purchase</FinishButtonName>    
   </Screen2>  
</App1>
</MyRoot>

The XAML code is

<Window x:Class="WPF_XML.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="SomeStrings" Source="pack://siteoforigin:,,,/somestrings.xml"  XPath="MyRoot/App1/Common|MyRoot/App1/Screen1"/>

    </Window.Resources>  
    <Grid >
        <Label Content="{Binding Source={StaticResource SomeStrings}, XPath=ApplicationName}" Height="28" HorizontalAlignment="Left" Margin="38,82,0,0" Name="lblName" VerticalAlignment="Top" Width="107" />
        <Button Content="{Binding Source={StaticResource SomeStrings}, FallbackValue=oops, XPath=SelectGameshButtonName}" Height="24" HorizontalAlignment="Left" Margin="169,82,0,0" Name="btnSelect" VerticalAlignment="Top" Width="104" />
    </Grid>
</Window>

Using XMLDataProvider I am trying to assign display text to the controls from xml file. So Here are the observations I have,

XPath="MyRoot/App1/Common"  then label gets value.
XPath="MyRoot/App1/Screen1"  then button gets value

Since I want both of the control should get value in single query so use the union of XPath as,

XPath="MyRoot/App1/Common|MyRoot/App1/Screen1"

But i see only label is getting updated.

Why XMLDataProvider is failing to return the button name to the binding.

Is this an issue with XMLDataProvider or i am missing something?

Thanks

EDIT

Although setting XPath="MyRoot/App1" at XMLDataProvider and setting control content binding as below,

XPath="Common/ApplicationName|Screen1/ApplicationName" 
XPath="Common/SelectGameshButtonName|Screen1/SelectGameshButtonName" 

works functionally ! But I do not want to use this approach,

  1. Reason#1

from performance perspective. It will load all screens xml node in XMLDataProvider instead of just the common and screen1.

  1. Reason#2

Developer working on the screens should only use node name for the control without any prefix specification. They should not care where the string is located. Because over the time the strings may get moved to common.

Your XmlDataProvider return multiple items, but since you use UI controls for displaying single item (Label and Button, as opposed to ListBox, ItemsControl, etc) you get only the first item displayed by both UI controls (the <Common>...</Common> element).

The problem is not because of using XPath union. Even if you don't use union, if you have multiple elements with the same name in the XML, using your approach only the first element will be displayed.

To fix the problem, you can declare the XmlDataProvider using this XPath :

<XmlDataProvider XPath="MyRoot/App1" x:Key="SomeStrings" Source="pack://siteoforigin:,,,/somestrings.xml"  />

Then use following XPath for your Label and Button :

<Label Content="{Binding Source={StaticResource SomeStrings}, XPath=Common/ApplicationName}" Height="28" HorizontalAlignment="Left" Margin="38,82,0,0" Name="lblName" VerticalAlignment="Top" Width="107" />
<Button Content="{Binding Source={StaticResource SomeStrings}, FallbackValue=oops, XPath=Screen1/SelectGameshButtonName}" Height="24" HorizontalAlignment="Left" Margin="169,82,0,0" Name="btnSelect" VerticalAlignment="Top" Width="104" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM