如何从扩展函数返回空节点集? [英] How to return empty node-set from extension function?

查看:68
本文介绍了如何从扩展函数返回空节点集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从扩展函数返回空节点集?

How do I return a empty node-set from an extension function?

不接受Null。

XPathNavigator GetResult( ...)
{
  ...
  else返回null;
}

XPathNavigator GetResult(...)
{
  ...
  else return null;
}

我可以返回新的XDocument()。CreateNavigator()但它与空节点集不完全相同。还有什么呢? XPathNavigator.Empty不存在。

I could return new XDocument().CreateNavigator() but its not exactly the same as an empty node-set. What else could there be? XPathNavigator.Empty does not exist.

推荐答案

使用XPath,您可以执行"/..&";得到一个空的节点集,这样做

With XPath you do "/.." to get an empty node-set so doing

 返回新的XPathDocument(新的StringReader("< foo />"))。CreateNavigator()。选择(") ..")

 return new XPathDocument(new StringReader("<foo/>")).CreateNavigator().Select("..")

应该足够了。这样做看起来有点尴尬,但我不确定是否有更短的东西。当然,如果你想创建一个XmlDocument或XDocument来创建XPathNavigator并执行Select(".."),那么你也可以这样做。

should suffice. It looks a bit akward to have to do that but I am not sure there is something shorter. Of course if you prefer to create an XmlDocument or XDocument to create an XPathNavigator on and do Select("..") then you can do that too.


这篇关于如何从扩展函数返回空节点集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆