2014-02-25 102 views
1

我有一些XML存儲在數據庫中,並以字符串形式傳遞給我的方法。XPath找不到節點

<settings> 
    <ReportOptions> 
    <ReportWidget> 
     <widget Name="Report Exclusions"> 
     <Visibility> 
      <VisibilitySetting Name="ClosedLocations" Value="true"/> 
     </Visibility> 
     </widget> 
     <widget Name="Report Period"> 
     <Visibility> 
      <VisibilitySetting Name="Day" Value="true"/> 
      <VisibilitySetting Name="Month" Value="true"/> 
     </Visibility> 
     </widget> 
     <widget Name="Output Method"> 
     <Properties> 
      <Visibility> 
      <VisibilitySetting Name="Report Viewer" Value="true"/> 
      <VisibilitySetting Name="Pdf" Value="true"/> 
      </Visibility> 
     </Properties> 
     </widget> 
    </ReportWidget> 
    </ReportOptions> 
</settings> 

我運行一個在線的XPath測試下面的表達式:

/settings/ReportOptions/ReportWidget/widget[@Name='Output Method']/Properties/Visibility/VisibilitySetting[@Name] 

,我得到下面的結果:

<?xml version="1.0" encoding="UTF-8"?> 
<result> 
<VisibilitySetting Name="Report Viewer" Value="true"/> 

<VisibilitySetting Name="Pdf" Value="true"/> 

</result> 

但是當我運行下面的代碼:

var customizationApplied = false; 
var firstProperty = Items[0]; 
var settings = firstProperty.Widget.Report.Settings; 
var widgetName = firstProperty.Widget.Name; 

// Select the node and place the results in an iterator 
var strExpression =  settings.Compile("/settings/ReportOptions/ReportWidget/widget[@Name='Output Method']/Properties/Visibility/VisibilitySetting[@Name]"); 


var nodeIterator = settings.Select(strExpression); 

// iterate through the results 
while (nodeIterator.MoveNext()) 
{ 
    var xmlPropertyName = nodeIterator.Current.GetAttribute("Name", string.Empty); 

    var propertyToCustomize = (from prop in Items 
           where string.Equals(prop.Name, xmlPropertyName, StringComparison.OrdinalIgnoreCase) 
           select prop).FirstOrDefault(); 

    if (propertyToCustomize != null) 
    { 
     var xmlPropertyValue = nodeIterator.Current.GetAttribute("Value", string.Empty); 
     propertyToCustomize.IsEnabled = StringToObjectHelper.GetValue(xmlPropertyValue, false); 

     customizationApplied = true; 
    } 
} 

我得不到結果。 nodeIterator總是在鼠標懸停上顯示:

Position=0, Current={Root} 

當我執行nodeIterator.MoveNext()方法時,它返回false。

任何人都可以請說明問題可能是什麼?這一切都工作過,沒有任何改變。

感謝, 李

+1

請在此處添加您正在使用的技術的標籤。您不僅僅使用XPath。 JavaScript的?還有什麼?如果您沒有正確標記,那麼可以回答您問題的人將無法找到它。 – helderdarocha

回答

0

爲什麼不使用標準的.NET的LINQ到XML:

using System.Xml.XPath; 
using System.Xml.Linq; 

var xml = @"<settings> 
    <ReportOptions> 
    <ReportWidget> 
     <widget Name=""Report Exclusions""> 
     <Visibility> 
      <VisibilitySetting Name=""ClosedLocations"" Value=""true""/> 
     </Visibility> 
     </widget> 
     <widget Name=""Report Period""> 
     <Visibility> 
      <VisibilitySetting Name=""Day"" Value=""true""/> 
      <VisibilitySetting Name=""Month"" Value=""true""/> 
     </Visibility> 
     </widget> 
     <widget Name=""Output Method""> 
     <Properties> 
      <Visibility> 
      <VisibilitySetting Name=""Report Viewer"" Value=""true""/> 
      <VisibilitySetting Name=""Pdf"" Value=""true""/> 
      </Visibility> 
     </Properties> 
     </widget> 
    </ReportWidget> 
    </ReportOptions> 
</settings>"; 
var doc = XDocument.Parse(xml); 
var nodes = 
    doc.XPathSelectElements(
     "/settings/ReportOptions/ReportWidget/widget[@Name='Output Method']/Properties/Visibility/VisibilitySetting[@Name]"); 
foreach (var node in nodes) 
{ 
    Console.WriteLine(node.ToString()); 
} 

輸出是當你從網上的XPath測試得到了完全相同的:

<VisibilitySetting Name="Report Viewer" Value="true"/> 
<VisibilitySetting Name="Pdf" Value="true"/>