XDocument 中的 Xmldocument 等效项
我使用 XMLDocument 编写了以下代码
string Query = @"/ShortcutList/" + Modality;
XmlNodeList nodes = shortcutsXMLDocument.SelectNodes(Query);
if (nodes == null)
{
// if the modality not exists, I will load the Default one
Query = @"/ShortcutList/Default";
nodes = shortcutsXMLDocument.SelectNodes(Query);
}
for (int i = 0; i < nodes[0].ChildNodes.Count; i++)
{
// do something here
}
,其中 shortcutsXMLDocument
是 XMLDocument
如何使用 xDocument 进行转换,我未能在 xdocument 中找到与 SelectNodes 等效的内容,
请有任何想法
I wrote the following code using XMLDocument
string Query = @"/ShortcutList/" + Modality;
XmlNodeList nodes = shortcutsXMLDocument.SelectNodes(Query);
if (nodes == null)
{
// if the modality not exists, I will load the Default one
Query = @"/ShortcutList/Default";
nodes = shortcutsXMLDocument.SelectNodes(Query);
}
for (int i = 0; i < nodes[0].ChildNodes.Count; i++)
{
// do something here
}
where shortcutsXMLDocument
is XMLDocument
How to convert it using xDocument , I failed to find the equivalent to SelectNodes in xdocument
any idea please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码相当奇怪,因为 SelectNodes 总是返回一个 XmlNodeList,因此检查
if (nodes == null)
永远不会为 true,您也可以将其删除。对于前两行,您可以
假设
Modality
变量仅包含元素名称而不是完整的 XPath 表达式来替换它们。然后,对于
for
循环,您可以使用例如,但我怀疑,如果您通过发布 XML 示例以及对哪些数据的一些解释来告诉我们您想要实现的目标,我们可以编写更清晰、更简单的代码。想要提取。
Well the code is rather odd as SelectNodes always returns an XmlNodeList so the check
if (nodes == null)
will never be true and you can just as well remove it.As for the first two lines, you could replace them with
assuming that the
Modality
variable contains simply an element name and not a complete XPath expression.Then for the
for
loop you could use e.g.but I suspect that we could write clearer and simpler code if you tell us what you want to achieve by posting a sample of the XML and some explanation of which data you want to extract.