XML 文件元素中的属性 'xmlns' 导致 XmlDataProvider 无法读取节点

Attribute 'xmlns' in XML file element causes XmlDataProvider to not read past the node

我正在尝试读取和解析 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' 级别或以下级别,我就不会在 return.

中获得任何数据

这是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 文件的格式,因此必须保留有问题的属性。

您需要在 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>