使用 linq 从 xml 树获取属性

发布于 2024-08-14 05:42:40 字数 1576 浏览 3 评论 0原文

我正在使用一个如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜警司 2024-08-21 05:42:40

使用 linq-to-xml 向上(父/祖先)或向下(元素/元素/后代)导航树。有关详细信息,请参阅 msdn

XDocument doc

var q = from element5 in doc.Elements("element5")
        let element4 = element5.Parent
        let element6 = element5.Element("element6")
        let element7 = element6.Element("element7")
        select new {
                     attr1 = (DateTime)element4.Attribute("attr1"),
                     attr2 = (string)element5.Attribute("attr2"),
                     attr3 = (int)element6.Attribute("attr3"),
                     attr4 = (int)element7.Attribute("attr4")
                     attr5 = (float)element7.Attribute("attr5")
                     attr6 = (float)element7.Attribute("attr6")
                   }

Use linq-to-xml to navigate the tree up (parent/ancestors) or down (element/elements/descendants). See msdn for details.

XDocument doc

var q = from element5 in doc.Elements("element5")
        let element4 = element5.Parent
        let element6 = element5.Element("element6")
        let element7 = element6.Element("element7")
        select new {
                     attr1 = (DateTime)element4.Attribute("attr1"),
                     attr2 = (string)element5.Attribute("attr2"),
                     attr3 = (int)element6.Attribute("attr3"),
                     attr4 = (int)element7.Attribute("attr4")
                     attr5 = (float)element7.Attribute("attr5")
                     attr6 = (float)element7.Attribute("attr6")
                   }
鹤仙姿 2024-08-21 05:42:40
xdoc = XDocument.Load(Server.MapPath("Temp.xml"))
Dim x = From el As XElement In xdoc...<vehicles>.Descendants.Where(Function(f) [email protected] = id.ToString)
Dim at = From a In x.Attributes()
For Each t In at
Dim n = t.Name
Dim v = t.Value
ProcessForm(n.ToString, v)
Next
xdoc = XDocument.Load(Server.MapPath("Temp.xml"))
Dim x = From el As XElement In xdoc...<vehicles>.Descendants.Where(Function(f) [email protected] = id.ToString)
Dim at = From a In x.Attributes()
For Each t In at
Dim n = t.Name
Dim v = t.Value
ProcessForm(n.ToString, v)
Next
‖放下 2024-08-21 05:42:40

不完全清楚,但这可能是一个起点:

XElement el = // something here
el.Descendants().Concat(new XElement[]{el}).SelectMany(e => e.Attributes())

我认为我没有确切地找到您正在寻找的东西...您听起来像是从对 element5 的引用开始,并且您想要向上移动下树?

编辑:我认为这可能就是您正在寻找的(再次阅读您的问题后):

XElement el = // something here
el.Descendants().Concat(new XElement[]{el}).Where(e => e.Name.LocalName == "element5").SelectMany(e => e.Attributes())

Not perfectly clear, but this might be a starting point:

XElement el = // something here
el.Descendants().Concat(new XElement[]{el}).SelectMany(e => e.Attributes())

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):

XElement el = // something here
el.Descendants().Concat(new XElement[]{el}).Where(e => e.Name.LocalName == "element5").SelectMany(e => e.Attributes())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文