使用XDocument迭代XML文件将返回没有属性的元素

时间:2015-12-09 09:32:08

标签: c# xml

我正在遍历此XML文件的所有子元素:

git checkout ESLINT
git checkout -b ESLINT-rebase-test
git rebase DEV

使用此代码:

<?xml version="1.0" encoding="utf-8"?>
<users>
    <user name="SemiViral" access="2" />
</users>

XDocument doc = XDocument.Load("Users.xml"); Console.WriteLine(doc.Descendants("users").Count()); foreach (XElement u in doc.Descendants("users")) { Console.WriteLine(u.Attributes().Count()); } 的输出为WriteLine,如果我尝试直接引用属性,则类似为空。计算后代返回0,当我将内部内容添加到单个子元素时,它能够输出这些内容。所以我知道这是正确的元素,因为某些原因它根本就没有访问属性。

1 个答案:

答案 0 :(得分:0)

这是一个代码,可以执行您要执行的操作。您没有获得结果,因为您只查找app.get('/', function (req, res) { res.sendFile(__dirname + '/client/wwww/templates/'); }); app.get('/main', function (req, res) { res.sendFile(__dirname + '/client/www/templates/main.html'); }); 元素(users)。您正在寻找的元素位于xml的下一级。如果你调试了代码,你就会发现它。

doc.Descendants("users")
相关问题