在 LINQ select 语句中使用 xml 父数据

发布于 2024-08-20 07:27:00 字数 804 浏览 5 评论 0原文

假设我已经有了这个 XML:

<items> 
  <item name="thumb"> 
    <downloadStream>test1</downloadStream> 
    <downloadStream>test2</downloadStream> 
    <downloadStream>test3</downloadStream> 
  </item> 
  <item name="photo"> 
    <downloadStream>test5</downloadStream> 
    <downloadStream>test6</downloadStream> 
    <downloadStream>test7</downloadStream> 
  </item> 
</items> 

我正在尝试编写一个 LINQ 语句,它将把它转换为以下字符串:

{ "thumb test1",
  "thumb test2",
  "thumb test3",
  "photo test5",
  "photo test6",
  "photo test7", }

换句话说,它将父节点的属性附加到每个子节点的内部字符串。

他们是我可以使用一个 LINQ 查询来做这样的事情的一种方式吗?我可以找到几种方法将其分解为多个步骤,但我有一种感觉,这是一种更简单的方法。

谢谢!

lets say that I've got this XML:

<items> 
  <item name="thumb"> 
    <downloadStream>test1</downloadStream> 
    <downloadStream>test2</downloadStream> 
    <downloadStream>test3</downloadStream> 
  </item> 
  <item name="photo"> 
    <downloadStream>test5</downloadStream> 
    <downloadStream>test6</downloadStream> 
    <downloadStream>test7</downloadStream> 
  </item> 
</items> 

I'm trying to write a LINQ statement which will convert this to the following strings:

{ "thumb test1",
  "thumb test2",
  "thumb test3",
  "photo test5",
  "photo test6",
  "photo test7", }

In other words, it appends the attribute from the parent node to the inner-string of each child node.

Is their a way that I can use one LINQ query to do something like this? I can find a few ways to break it up into multiple steps, but I have a feeling that their is an easier way.

Thanks!

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

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

发布评论

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

评论(3

傻比既视感 2024-08-27 07:27:00
XDocument.Load(myXML)
    .Descendants("item")
    .SelectMany(d => d.Descendants()
        .Select(ds => d.Attribute("name").Value + " " + ds.Value));
XDocument.Load(myXML)
    .Descendants("item")
    .SelectMany(d => d.Descendants()
        .Select(ds => d.Attribute("name").Value + " " + ds.Value));
熟人话多 2024-08-27 07:27:00

这是 VB.NET 中的。同样的事情在 C# 中也是可能的,但我更熟悉 VB 语法。

    Dim itemsXml = <items>
                       <item name="thumb">
                           <downloadStream>test1</downloadStream>
                           <downloadStream>test2</downloadStream>
                           <downloadStream>test3</downloadStream>
                       </item>
                       <item name="photo">
                           <downloadStream>test5</downloadStream>
                           <downloadStream>test6</downloadStream>
                           <downloadStream>test7</downloadStream>
                       </item>
                   </items>
    Dim itemQuery = From ds In itemsXml...<downloadStream> _
                    Select ds.Parent.@name & " " & ds.Value

Here it is in VB.NET. Same thing would be possible in C#, but I'm more familiar with the VB syntax.

    Dim itemsXml = <items>
                       <item name="thumb">
                           <downloadStream>test1</downloadStream>
                           <downloadStream>test2</downloadStream>
                           <downloadStream>test3</downloadStream>
                       </item>
                       <item name="photo">
                           <downloadStream>test5</downloadStream>
                           <downloadStream>test6</downloadStream>
                           <downloadStream>test7</downloadStream>
                       </item>
                   </items>
    Dim itemQuery = From ds In itemsXml...<downloadStream> _
                    Select ds.Parent.@name & " " & ds.Value
狼性发作 2024-08-27 07:27:00
var list = x.Descendants("item")
            .SelectMany(item => item.Elements("downloadStream")
                                    .Select(e => (string)item.Attribute("name") 
                                                 + " " 
                                                 + (string)e)).ToList();
var list = x.Descendants("item")
            .SelectMany(item => item.Elements("downloadStream")
                                    .Select(e => (string)item.Attribute("name") 
                                                 + " " 
                                                 + (string)e)).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文