C# - LinQ to XML - 每个元素打印XElement行 [英] C# - LinQ to XML - print XElement line per element

查看:65
本文介绍了C# - LinQ to XML - 每个元素打印XElement行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我有一个关于LinQ to XML和迭代列表的问题。



这是我到目前为止的代码:



Hi everyone!

I have a question regarding LinQ to XML and iterating through a list.

This is my code so far:

new XElement("caption", data.ToList().Select(x => x))





这样做是打印一个包含列表中所有内容的XElement语句data 。我的意图是在列表中每个元素都有一个XElement语句(以递归方式排序)。谁能解释一下我做错了什么?



谢谢大家!



What this is doing is printing an XElement statement with everything in the list "data". What my intent is is to have one XElement statement per element in the list (sort of recursive fashion). Can anyone shed some light as to what I''m doing wrong?

Thanks everyone!

推荐答案

您需要遍历数据并为每个项目生成一个XElement。

You need to iterate through data and yield an XElement for each item.
var result = new XElement("captions", data.Select(x => new XElement("caption", x)));





这将生成结果如,





This will generate the result like,

<captions>
  <caption>a</caption> 
  <caption>b</caption> 
  <caption>c</caption> 
</captions>





在你的观点中,假设有一个节点



In your scenarion it assumes, there is a single node

<caption></caption>

,其值为

data.ToList().Select(x => x)


这篇关于C# - LinQ to XML - 每个元素打印XElement行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆