使用 linq to xml 获取元素中的多个数据项

发布于 2024-08-31 18:45:09 字数 441 浏览 3 评论 0原文

使用 linq to xml 获取元素中的多个数据项

我有一个像这样的 xml 文件,

<TopLevel>
  <Inside>
    Jibba
  </Inside>
  <Inside>
    Jabba
  </Inside>
</TopLevel>

我得到了所说的 xml 并且想要获取所有元素。这是我的代码。

    var q = from c in loaded.Descendants("TopLevel")
            select (XElement)c.Element("Inside");

我尝试了 c.Elements 但没有成功。我需要做什么才能获得所有内部元素?此代码仅获取第一个“内部”标签。

Getting multiple data items in an element with linq to xml

I have an xml file like this

<TopLevel>
  <Inside>
    Jibba
  </Inside>
  <Inside>
    Jabba
  </Inside>
</TopLevel>

I was given said xml and and want to get all the elements. Here is the code I have.

    var q = from c in loaded.Descendants("TopLevel")
            select (XElement)c.Element("Inside");

I tried c.Elements but that did not work. What do I need to do to get all of the internal elements? This code just gets the first "Inside" tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

许仙没带伞 2024-09-07 18:45:09

这应该为您提供 "Jibba""Jabba";您需要为 System.LinqSystem.Xml.Linq 使用 using 指令:

    var q = from c in loaded.Descendants("TopLevel").Elements("Inside")
            select c.Value;

或(不太具体,但仍然有效):

    var q = from c in loaded.Descendants("TopLevel").Elements()
            select c.Value;

或如果您想对元素执行更多操作,SelectMany:(

    var q = from c in loaded.Descendants("TopLevel")
            from i in c.Elements("Inside")
            select i.Value;

如果您想要元素而不是字符串,则只需 select c;select i; - 删除.Value

This should give you "Jibba" and "Jabba"; you'll need using directives for both System.Linq and System.Xml.Linq:

    var q = from c in loaded.Descendants("TopLevel").Elements("Inside")
            select c.Value;

or (less specific, but still works):

    var q = from c in loaded.Descendants("TopLevel").Elements()
            select c.Value;

or if you want to do something more with the elements, SelectMany:

    var q = from c in loaded.Descendants("TopLevel")
            from i in c.Elements("Inside")
            select i.Value;

(if you want the element, not the string, then just select c; or select i; - remove the .Value)

dawn曙光 2024-09-07 18:45:09

我需要更进一步,但不太清楚如何去做。感谢您的出发点。这里有一些额外的水果可以节省一些劳力。

给定一些额外的 xml 乐趣:

<TopLevel>
    <Inside ident="one">Jibba</Inside>
    <Inside ident="two">Jabba</Inside>
    <Inside ident="one">AlsoJibba</Inside>
    <Inside ident="three">AlsoJabba</Inside>
</TopLevel>

我添加了一个 where 子句,用于查找父级“TopLevel”中 ident 属性值为“one”的元素。

Dim query = From c In loaded.Descendants("TopLevel").Elements("inside")
            Where c.Attribute("ident") = "one"
            Select c.Value

然后对结果进行了一些演示控制台运行:

For each inside in query
    Console.WriteLine(inside)
Next

哪个输出:

Jibba
AlsoJibba

前方剧透警报:

现在,如果您复制粘贴我刚刚所做的事情,并且您像我一样使用 VB,那么您会遇到一个特别烦人的细微差别。该代码不起作用。请注意 LINQ 查询中的小部分“.Elements("inside")。XML 区分大小写。VB 极其不区分大小写。C# 用户请随意忽略我们的年轻人。只是想演示一个位置和一个陷阱。

马特

I needed to take this a step further and wasn't real clear as to how to do it. Thanks for the jump off point. Here's some additional fruit to save some labor.

Given some additional xml fun:

<TopLevel>
    <Inside ident="one">Jibba</Inside>
    <Inside ident="two">Jabba</Inside>
    <Inside ident="one">AlsoJibba</Inside>
    <Inside ident="three">AlsoJabba</Inside>
</TopLevel>

I added a where clause that looks for elements inside the parent "TopLevel" that have an ident attribute value of "one".

Dim query = From c In loaded.Descendants("TopLevel").Elements("inside")
            Where c.Attribute("ident") = "one"
            Select c.Value

Then did a little demo console run of the results:

For each inside in query
    Console.WriteLine(inside)
Next

Which output:

Jibba
AlsoJibba

Spoiler Alert Ahead:

Now if you copy pasted what I just did and you are using VB like I am you have an especially annoying nuance. The code doesn't work. Notice the little section ".Elements("inside") in the LINQ query. XML is case sensitive. VB is extremely not case sensitive. C# users please feel free to ignore our youth. Just wanted to demonstrate a where, and a gotcha.

Matt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文