使用 linq 从 xml 树获取属性
我正在使用一个如下所示的 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<element1 xmlns="http://namespace1/">
<element2>
<element3>
<element4 attr1="2009-11-09">
<element5 attr2="NAME1">
<element6 attr3="1">
<element7 attr4="1" attr5="5.5" attr6="3.4"/>
</element6>
</element5>
<element5 attr2="NAME2">
<element6 attr3="1">
<element7 attr4="3" attr5="4" attr6="4.5"/>
</element6>
</element5>
</element4>
</element3>
</element2>
</element1>
我需要循环遍历 element5 并检索 Ienumberable 中的属性,如下所示:
attr1、attr2、attr3、attr4、attr5、attr6
使用 linq to xml 和 c#。我可以循环使用 element5 并获取所有 attribute2 信息,但我不知道如何获取我需要的父属性或子属性。
更新:感谢迄今为止的反馈。为了清楚起见,我需要循环访问 attribute5。所以基本上,我现在拥有的(不多)是 . 。 。
XElement xel = XElement.Load(xml);
IEnumberable<XElement> cList = from el in xel.Elements(env + "element2").Element
(n2 + "element3").Elements(n2 + "element4").Elements(ns + "element5") select el;
foreach (XElement e in cList)
Console.WriteLine(e.Attribute("attr2").Value.ToString());
这将为我提供循环中所有 attr 2 的值,但对于我想要实现的目标,我可能会做所有错误的事情。我还需要将上面提到的其他属性收集到一个集合中(控制台参考只是我现在正在玩这个,但我需要的最终结果是一个集合)。 这样的集合吗?
attr1, attr2, attr3, attr4, attr5, attr6
2009-11-09, name1, 1, 1, 5.5, 3.4
2009-11-09, name2, 1, 3, 4, 4.5
那么最终的结果会是像Make Sense
I'm working with an xml file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<element1 xmlns="http://namespace1/">
<element2>
<element3>
<element4 attr1="2009-11-09">
<element5 attr2="NAME1">
<element6 attr3="1">
<element7 attr4="1" attr5="5.5" attr6="3.4"/>
</element6>
</element5>
<element5 attr2="NAME2">
<element6 attr3="1">
<element7 attr4="3" attr5="4" attr6="4.5"/>
</element6>
</element5>
</element4>
</element3>
</element2>
</element1>
Where I need to loop through element5 and retrieve the attributes in an Ienumberable like this:
attr1, attr2, attr3, attr4, attr5, attr6
using linq to xml and c#. I can loop through the element5 and get all the attribute2 info using but I can't figure out how to get the parent or child attributes I need.
UPDATE: Thanks for the feeback thus far. For clarity, I need to do a loop through attribute5. So basically, what I have right now (which isn't much) is . . .
XElement xel = XElement.Load(xml);
IEnumberable<XElement> cList = from el in xel.Elements(env + "element2").Element
(n2 + "element3").Elements(n2 + "element4").Elements(ns + "element5") select el;
foreach (XElement e in cList)
Console.WriteLine(e.Attribute("attr2").Value.ToString());
This will give me the value all the attr 2 in the loop but I could be going about this all wrong for what I'm trying to acheive. I also need to collect the other attributes mentioned above in a collection (the Console reference is just me playing with this right now but the end result I need is a collection). So the end results would be a collection like
attr1, attr2, attr3, attr4, attr5, attr6
2009-11-09, name1, 1, 1, 5.5, 3.4
2009-11-09, name2, 1, 3, 4, 4.5
Make Sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 linq-to-xml 向上(父/祖先)或向下(元素/元素/后代)导航树。有关详细信息,请参阅 msdn。
Use linq-to-xml to navigate the tree up (parent/ancestors) or down (element/elements/descendants). See msdn for details.
不完全清楚,但这可能是一个起点:
我认为我没有确切地找到您正在寻找的东西...您听起来像是从对 element5 的引用开始,并且您想要向上移动下树?
编辑:我认为这可能就是您正在寻找的(再次阅读您的问题后):
Not perfectly clear, but this might be a starting point:
I don't think I have exactly what you're looking for... You make it sound like you are starting with a reference to an element5 and you want to go up and down the tree?
EDIT: I think this might be what you're looking for (after reading your question yet again):