【发布时间】:2017-11-08 13:38:36
【问题描述】:
所以我有一个具有以下结构的 xml 文件:
<Group Name = "Groceries">
<Lifetime Creation="2015" Expiration="2018" />
<Brown Vegetables>
<Potatoes>
<LifeTime Creation ="0" Expiration ="0"/>
</Potatoes>
</Brown Vegetables>
<Green Vegetables>
<Lettuce>
<LifeTime Creation ="1" Expiration ="1"/>
</Lettuce>
<Green Vegetables>
</Group>
所以 Group Name 被认为是父杂货。 我想从包含名称“杂货”的组名中获取 LifeTime 的属性值, 将它们存储在两个整数中,并将这两个整数添加到 Groupname Groceries 下每个孩子的 LifeTime
输出:
<Group Name = "Groceries">
<Lifetime Creation="2015" Expiration="2018" />
<Brown Vegetables>
<Potatoes>
<LifeTime Creation ="2015" Expiration ="2018"/>
</Potatoes>
</Brown Vegetables>
<Green Vegetables>
<Lettuce>
<LifeTime Creation ="2016" Expiration ="2019"/>
</Lettuce>
<Green Vegetables>
</Group>
问题是: 我不知道 xml 的完整结构,因此在 Group Name 上方可能还有一些其他父节点。我只知道命名空间
我尝试过这样的事情::
var value = (from c in doc.Descendants("Position") curr.Attribute("X").Value == "");
但我得到的只是空值..所以读数不正确
【问题讨论】:
-
这是在开玩笑吗?
-
没有。我只是 xml 解析的新手
-
您正在寻找一个名为“位置”的元素?为什么?
-
我的错,正在解析另一个 xml 文档并从那里取行。因此,从我从 MSDN 中读到的内容来看,在我的情况下,后代将是 Descendands(Group) 和 curr.Attribute(Creation) 和 (Expiration) 类似的东西
-
尝试以下操作: var elements = doc.Descendants().Where(x => x.Attributes().Select(y => (string)y == "").Any()) .ToList();
标签: c# xml xml-parsing