我有一些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>
我在在线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。
有谁可以请问一下问题可能是什么?这一切都在以前工作,没有任何改变。
谢谢, 利
答案 0 :(得分:0)
为什么不使用标准.NET的LINQ-to-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"/>