开发者

Linq to XML when data held in a list

I have a list of strings that I ne开发者_StackOverflow中文版ed to use to create the following XML. The items in the list are strings "Line 1", "Line 2" etc. The tricky bit is that the element names increment from "l1" upwards. Is it possible to use Linq to do this or should I use a different approach ?

<srv>
  <enqRsp>
    <l1>LINE 1</l1>
    <l2>LINE 2</l2>
    <l3>LINE 3</l3>
    <l4>LINE 3</l4>
  </enqRsp>
</srv>


This is totally possible, using the Select overload which provides the index as well as the value:

var document = new XDocument(new XElement("srv",
     new XElement("enqRsp",
        list.Select((value, index) => new XElement("l" + (index+1), value))
     )
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜