我正在尝试构建一个 Linq to XML Query,但还没有找到真正的解决方案。
这是我的 XML
<Nodes>
<Node Text="Map" Value="Map">
<Node Text="12YD" Value="12YD">
<Node Text="PType" Value="PType">
<Node Text="12" Value="12" />
</Node>
<Node Text="SType" Value="SType">
<Node Text="2" Value="2" />
</Node>
</Node>
<Node Text="12YP" Value="12YP">
<Node Text="PType" Value="PType">
<Node Text="12" Value="12" />
</Node>
<Node Text="SType" Value="SType">
<Node Text="1" Value="1" />
</Node>
</Node>
</Node>
</Nodes>
我可用的参数用于 PType 节点和 SType 节点,现在取决于它们的值,我需要获取父节点属性值。
Example:
Params: {PType:12}, {SType:2} should give me 12YD as a result.
Params: {PType:12}, {SType:1} should give me 12YP as a result.
即使使用 PredicateBuilder,我也尝试过不同的解决方案,但没有成功。任何帮助,将不胜感激。
这是我使用 LinqPad 的最新代码。
void Main()
{
var xml = XElement.Load (@"C:\map.xml");
string value = "{PType:12},{SType:1}";
string[] mapReqValues = value.Split(',');
var predicate = PredicateBuilder.False<XElement>();
foreach (string r in mapReqValues)
{
var m = Regex.Match(r, @"{([^}]+)}").Groups[1].Value.Split(':');
predicate = predicate.Or(p => p.Attribute("Value").Value == m[0] &&
p.Descendants().Attributes("Value").FirstOrDefault().Value == m[1]);
}
var result = xml.Descendants().AsQueryable().Where(predicate);
result.Dump();
}