LINQ to XML is indeed the way here:
LINQ to XML确实就是这样的方式:
var textArray = topElement.Elements("L")
.Select(x => x.Value)
.ToArray();
var text = string.Join(" ", textArray);
EDIT: Based on the comment, it looks like you just need a single-expression way of representing this. That's easy, if somewhat ugly:
编辑:基于评论,看起来你只需要一种表达方式的单表达方式。这很容易,如果有点难看:
result = (from indexentry in source.Elements(entryLevel)
select new IndexEntry
{
EtiologyCode = indexentry.Element("IE") == null
? null
: indexentry.Element("IE").Value,
//some code to set other properties in this object
Note = string.Join(" ", indexentry.Elements("NO")
.Descendants()
.Select(x => x.Value)
.ToArray())
};
Another alternative is to extract it into a separate extension method (it has to be in a top-level static class):
另一种方法是将其提取为单独的扩展方法(它必须位于顶级静态类中):
public static string ConcatenateTextNodes(
this IEnumerable elements)
{
string[] values = elements.Select(x => x.Value).ToArray();
// You could parameterise the delimiter here if you wanted
return string.Join(" ", values);
}
then change your code to:
然后将您的代码更改为:
result = (from indexentry in source.Elements(entryLevel)
select new IndexEntry
{
EtiologyCode = indexentry.Element("IE") == null
? null
: indexentry.Element("IE").Value,
//some code to set other properties in this object
Note = indexentry.Elements("NO")
.Descendants()
.ConcatenateTextNodes()
}
EDIT: A note about efficiency
编辑:关于效率的说明
Other answers have suggested using StringBuilder
in the name of efficiency. I would check for evidence of this being the right way to go before using it. If you think about it, StringBuilder
and ToArray
do similar things - they create a buffer bigger than they need to, add data to it, resize it when necessary, and come out with a result at the end. The hope is that you won't need to resize too often.
其他答案建议在效率名称中使用StringBuilder。在使用它之前,我会检查这是正确的方法。如果你考虑一下,StringBuilder和ToArray会做类似的事情 - 他们创建一个比他们需要的更大的缓冲区,向它添加数据,在必要时调整它的大小,并在最后得出结果。希望你不需要经常调整大小。
The difference between StringBuilder
and ToArray
here is what's being buffered - in StringBuilder
it's the entire contents of the string you've built up so far. With ToArray
it's just references. In other words, resizing the internal buffer used for ToArray
is likely to be cheaper than resizing the one for StringBuilder
, particularly if the individual strings are long.
StringBuilder和ToArray之间的区别在于缓冲 - 在StringBuilder中它是你到目前为止构建的字符串的全部内容。使用ToArray它只是引用。换句话说,调整用于ToArray的内部缓冲区的大小可能比调整StringBuilder的大小更便宜,特别是如果单个字符串很长。
After doing the buffering in ToArray
, string.Join
is hugely efficient: it can look through all the strings to start with, work out exactly how much space to allocate, and then concatenate it without ever having to copy the actual character data.
在ToArray中执行缓冲之后,string.Join非常有效:它可以查看所有字符串以开始,确切地分配多少空间,然后连接它而无需复制实际的字符数据。
This is in sharp contrast to a previous answer I've given - but unfortunately I don't think I ever wrote up the benchmark.
这与我之前给出的答案形成鲜明对比 - 但不幸的是,我认为我没有写过基准。
I certainly wouldn't expect ToArray
to be significantly slower, and I think it makes the code simpler here - no need to use side-effects etc, aggregation etc.
我当然不希望ToArray明显变慢,我认为它使代码更简单 - 不需要使用副作用等,聚合等。