我正在使用WmHelp.XPath2库来评估配置中特定的用户xpath 2.0查询,但我需要XPath返回一个字符串。
fn:string(//*:Identity/*:User/*:Name/text())
下面的示例xml(有自定义命名空间,这就是在xpath查询中使用通配符的原因):
<RootElement>
<Identity>
<User>
<Name>username</Name>
</User>
</Identity>
.....
XPath将评估正常,但我需要它来评估字符串而不是NodeSet。我的代码如下所示:
XPath2Expression uname_lookup = XPath2Expression.Compile(_emailToSource);
if (uname_lookup.ReturnType != XPath2ResultType.String)
{
throw new ApplicationException("USERNAME_LOOKUP XPath query has invalid return type. Must have a return type of string.");
}
string username = (string)navigator.XPath2Evaluate(uname_lookup);
我每次都会抛出异常。我意识到XPath2ResultType枚举定义可能存在问题,Navigator和String被赋予相同的值,但是有没有人知道每次都让我的xpath计算为字符串的方法。
namespace Wmhelp.XPath2
{
public enum XPath2ResultType
{
Number = 0,
Navigator = 1,
String = 1,
Boolean = 2,
NodeSet = 3,
Any = 5,
Error = 6,
DateTime = 7,
Duration = 8,
AnyUri = 9,
QName = 10,
Other = 11,
}
}