2015-08-25 7 views
0

J'essaie de lire et d'analyser un fichier xml dans xaml et je cours dans un roadblock car l'un des éléments (teststandfileheader) du fichier xml contient un attribut pour « xmlns » comme suit:L'attribut 'xmlns' dans l'élément de fichier XML provoque XmlDataProvider à ne pas lire après le noeud

<?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> 

Quand je mets le XPath juste «/», je reçois tout le texte associé à tous les éléments. Dès que je tente de définir XPath à quelque chose au niveau ou au-dessous du niveau 'teststandfileheader', je ne reçois aucune donnée en retour.

Voici le 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> 

Si je supprime (ou même renomme) l'attribut dans le fichier XML à autre chose que « xmlns » alors tout fonctionne comme prévu. Des suggestions sur les raisons pour lesquelles cela peut se produire et comment cela peut-il être corrigé? Notez également que Je ne contrôle pas le format du fichier XML donc l'attribut problématique doit rester.

Répondre

1

Vous devez utiliser votre espace de noms dans l'expression XPath - votre expression actuelle n'est pas correcte car il n'y a aucun élément avec ce nom complet.

Par exemple, l'espace de noms map au préfixe p et inclure dans le 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> 
+0

Merci! Cela a fait l'affaire. – user3037471