0

我正在尝试读取和解析 xaml 中的 xml 文件并遇到障碍,因为 xml 文件中的元素之一(teststandfileheader)包含“xmlns”的属性,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<teststandfileheader type="Globals" xmlns="http://www.ni.com/TestStand/14.0.0/PropertyObjectFile">
  Test
  <typelist />
  <Data classname="Obj">
    <subprops>
      <DEVBOARD classname="Bool">
        <value>false</value>
      </DEVBOARD>
      <DEVICES_PRESENT_HW classname="Bool">
        <value>true</value>
      </DEVICES_PRESENT_HW>
    </subprops>
  </Data>
</teststandfileheader>

当我将 XPath 设置为“/”时,我得到了与所有元素关联的所有文本。一旦我尝试将 XPath 设置为等于或低于“teststandfileheader”级别的内容,我就不会得到任何数据作为回报。

这是 XAML:

<Window x:Class="DOTSDebugSelector.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tb="http://www.hardcodet.net/taskbar"
        xmlns:local="clr-namespace:DOTSDebugSelector"
        Title="MainWindow" Height="350" Width="525" Visibility="Visible" WindowStyle="ToolWindow">
    <Window.Resources>
        <XmlDataProvider x:Key="TsStationGlobals" x:Name="TsStationGlobals" XPath="/teststandfileheader/Data/subprops" Source="C:\ProgramData\National Instruments\TestStand 2014 (32-bit)\Cfg\StationGlobalsdebug.ini" IsInitialLoadEnabled="True" IsAsynchronous="False">
        </XmlDataProvider>
        <local:TextToBoolConverter x:Key="BoolConverter"/>
        <ContextMenu x:Key="MyMenu">
            <CheckBox Content="USING DEV BOARD" Checked="DevBoard_Checked" IsChecked="{Binding Source={StaticResource TsStationGlobals}, Converter={StaticResource BoolConverter}, UpdateSourceTrigger=PropertyChanged, XPath=DEVBOARD/@value}"></CheckBox>
            <CheckBox Content="USING TEST HARDWARE" Checked="TestHardware_Checked" IsChecked="{Binding Source={StaticResource TsStationGlobals}, Converter={StaticResource BoolConverter}, UpdateSourceTrigger=PropertyChanged, XPath=DEVICES_PRESENT_HW/@value}"></CheckBox>
        </ContextMenu>
    </Window.Resources>
    <Grid>
        <tb:TaskbarIcon IconSource="DOTSIcon.ico" ToolTipText="Click to Setup DOTS Debug Mode" MenuActivation="RightClick"  ContextMenu="{StaticResource MyMenu}" />
        <TextBox HorizontalAlignment="Left" Height="22" Margin="222,124,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource TsStationGlobals}, UpdateSourceTrigger=PropertyChanged, XPath=DEVBOARD}" VerticalAlignment="Top" Width="74"/>
        <TextBox HorizontalAlignment="Left" Height="22" Margin="222,151,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource TsStationGlobals}, UpdateSourceTrigger=PropertyChanged, XPath=DEVICES_PRESENT_HW}" VerticalAlignment="Top" Width="74"/>
    </Grid>
</Window>

如果我将 XML 文件中的属性删除(甚至重命名)为“xmlns”以外的属性,那么一切都会按预期工作。关于为什么会发生这种情况以及如何纠正它的任何建议?另请注意,我不控制 XML 文件的格式,因此必须保留有问题的属性。

4

1 回答 1

1

您需要在 XPath 表达式中使用命名空间 - 您当前的表达式不正确,因为没有具有该完全限定名称的元素。

例如,将命名空间映射到前缀p并将其包含在 XPath 中:

<XmlDataProvider XPath="/p:teststandfileheader/p:Data/p:subprops" Source="...">
    <XmlDataProvider.XmlNamespaceManager>
        <XmlNamespaceMappingCollection>
            <XmlNamespaceMapping 
               Uri="http://www.ni.com/TestStand/14.0.0/PropertyObjectFile" 
               Prefix="p" />
        </XmlNamespaceMappingCollection>
    </XmlDataProvider.XmlNamespaceManager>
</XmlDataProvider>
于 2015-08-25T16:10:30.267 回答