gpt4 book ai didi

c# - XDocument.XPathSelectElements : I can't seem get the xpath syntax right

转载 作者:行者123 更新时间:2023-11-30 22:25:36 25 4
gpt4 key购买 nike

我正在尝试弄清楚为什么我的 xpath 不会选择我指定的节点

我的 xpath 表达式是 //DefaultValue,所以我希望所有名为 DefaultValue 的元素都被选中

我的测试文件(缩减)是:

<?xml version="1.0" encoding="utf-8"?>
<SharedDataSet xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition">
<Description />
<DataSet Name="ddd">
<Query>
<DataSourceReference>xxx</DataSourceReference>
<DataSetParameters>
<DataSetParameter Name="p1">
<DefaultValue>baaaah</DefaultValue> <!-- this node should be selected eh? -->
</DataSetParameter>
<DataSetParameter Name="p2">
<DefaultValue>fooo</DefaultValue> <!-- this node should be selected too eh? -->
</DataSetParameter>
</DataSetParameters>
</Query>
</SharedDataSet>

代码是:

XmlNamespaceManager xn = new XmlNamespaceManager(new NameTable());
xn.AddNamespace("ns", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition");
xn.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
xn.AddNamespace("cl", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition");

XDocument document = XDocument.Parse(reportBuffer, LoadOptions.PreserveWhitespace);
IEnumerable<XElement> elements = document.XPathSelectElements(xpath, xn);

此时 elements.Count() 等于 0

谁能看出我的愚蠢之处?

我似乎无法让 xml 恶魔高兴...:-(

最佳答案

<DefaultValue>元素实际上绑定(bind)到命名空间 http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition .

<SharedDataSet> document 元素没有前缀声明,所以很容易错过。自 <DefaultValue>是后代,它继承命名空间。

如果要选择<DefaultValue>您需要调整 XPath 的元素:

//ns:DefaultValue

关于c# - XDocument.XPathSelectElements : I can't seem get the xpath syntax right,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12207036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com