将2 IEnumerable <xelement>格式化为1 List </xelement>

时间:2014-05-15 14:37:13

标签: c# xml

有这样的代码:

    string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><batch_execute_response xmlns:ns2=""http://api.forticom.com/1.0/""><photos_getAlbums_response><hasMore>false</hasMore><pagingAnchor>LTE2MTYxNjI3NzkzNTI6LTE2MTY4NTkwMjY2MTg=</pagingAnchor><album xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""userPhotoAlbumBean""><aid>803161459156</aid><user_id>795206706132</user_id><title>Тест</title><created>2014-05-14</created><created_ms>1400068737027</created_ms><photos_count>1</photos_count><like_count>0</like_count><liked_it>false</liked_it><like_summary><count>0</count><self>false</self><like_id>PnAasffDKjZekWr3wgeF2UlSM_8okvSSWx4LkvRmSVI</like_id><like_possible>false</like_possible><unlike_possible>false</unlike_possible></like_summary><attrs><flags>m,mp,d,ap</flags></attrs><type>PUBLIC</type><types><type>PUBLIC</type></types><type_change_enabled>true</type_change_enabled><comments_count>0</comments_count></album><album xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""userPhotoAlbumBean""><aid>803161428692</aid><user_id>795206706132</user_id><title>Разное</title><created>2014-05-14</created><created_ms>1400068722920</created_ms><photos_count>1</photos_count><like_count>0</like_count><liked_it>false</liked_it><like_summary><count>0</count><self>false</self><like_id>PnAasffDKjZekWr3wgeF2WlnPTxYKKjQ7yS_mmw8hOI</like_id><like_possible>false</like_possible><unlike_possible>false</unlike_possible></like_summary><attrs><flags>m,mp,d,ap</flags></attrs><type>PUBLIC</type><types><type>PUBLIC</type></types><type_change_enabled>true</type_change_enabled><comments_count>0</comments_count></album><album xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""userPhotoAlbumBean""><aid>803161401812</aid><user_id>795206706132</user_id><title>Рaзное</title><created>2014-05-14</created><created_ms>1400068698053</created_ms><photos_count>1</photos_count><like_count>0</like_count><liked_it>false</liked_it><like_summary><count>0</count><self>false</self><like_id>PnAasffDKjZekWr3wgeF2by9GQKUjg7Uo628QqhAWLk</like_id><like_possible>false</like_possible><unlike_possible>false</unlike_possible></like_summary><attrs><flags>m,mp,d,ap</flags></attrs><type>PUBLIC</type><types><type>PUBLIC</type></types><type_change_enabled>true</type_change_enabled><comments_count>0</comments_count></album><album xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""userPhotoAlbumBean""><aid>803161248468</aid><user_id>795206706132</user_id><title>Теcт</title><created>2014-05-14</created><created_ms>1400068631153</created_ms><photos_count>1</photos_count><like_count>0</like_count><liked_it>false</liked_it><like_summary><count>0</count><self>false</self><like_id>PnAasffDKjZekWr3wgeF2dQDZAMRUGRArCxgIuY3w7A</like_id><like_possible>false</like_possible><unlike_possible>false</unlike_possible></like_summary><attrs><flags>m,mp,d,ap</flags></attrs><type>PUBLIC</type><types><type>PUBLIC</type></types><type_change_enabled>true</type_change_enabled><comments_count>0</comments_count></album><album xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:type=""userPhotoAlbumBean""><aid>341775330772</aid><user_id>795206706132</user_id><title>Разное</title><created>2013-01-14</created><created_ms>1358190484157</created_ms><photos_count>5</photos_count><like_count>0</like_count><liked_it>false</liked_it><like_summary><count>0</count><self>false</self><like_id>PnAasffDKjZekWr3wgeF2R3_exHUzHoiZzqeTiq912o</like_id><like_possible>false</like_possible><unlike_possible>false</unlike_possible></like_summary><attrs><flags>m,mp,d,ap</flags></attrs><type>PUBLIC</type><types><type>PUBLIC</type></types><type_change_enabled>true</type_change_enabled><comments_count>0</comments_count></album></photos_getAlbums_response></batch_execute_response>";

    XElement xElement = XElement.Parse(xml);
    var aids = xElement.Descendants("aid");
    var titles = xElement.Descendants("title");

如何获得具有此类结构的新List<string> elements;

elements[0] = aids[0] + "|" + titles[0];

最好的方法是什么?

1 个答案:

答案 0 :(得分:2)

您正在寻找Zip方法

var elements = aids
     .Zip(titles, (a, t) => string.Join("|", (string) a, (string) t))
     .ToList();