不能将Descendants()或Elements()与xmlns一起使用 [英] Can't use Descendants() or Elements() with xmlns

查看:73
本文介绍了不能将Descendants()或Elements()与xmlns一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触XML,在尝试从spring.net配置文件中获取特定标签时遇到了一个奇怪的问题.尝试缩小测试xml文件的问题后,我发现应用了以下代码:

I'm new to working with XML, and I've encountered a weird problem while trying to get a specific tag from a spring.net configuration file. After trying to narrow down the problem with a test xml file, I found out that applying the following code:

List<XElement> nodes = xmlFile.Descendants("B").ToList();

提供带有以下文件的非空列表:

provides a non-empty list with the following file:

<?xml version="1.0" encoding="utf-8" ?>
<A fakeAttribute="aaa">
  <B id="DbProvider"/>
</A>

但是提供了一个包含以下文件的空字符串:

but provides an empty string with the following file:

<?xml version="1.0" encoding="utf-8" ?>
<A xmlns="aaa">
  <B id="DbProvider"/>
</A>

文件之间的唯一区别是属性.

The only difference between the files being the attribute.

我无法想象对此的解释.感谢您的帮助.

I can't imagine an explanation for this. Thanks for your help.

推荐答案

您需要在该命名空间中搜索标签:

You need to search for tags in that namespace:

XNamespace ns = "aaa";

xmlFile.Descendants(ns + "B").ToList()

这篇关于不能将Descendants()或Elements()与xmlns一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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