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。
任何人都可以請說明問題可能是什麼?這一切都工作過,沒有任何改變。
感謝, 李
請在此處添加您正在使用的技術的標籤。您不僅僅使用XPath。 JavaScript的?還有什麼?如果您沒有正確標記,那麼可以回答您問題的人將無法找到它。 – helderdarocha