C# linq to xml

XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("voteList",
new XAttribute("PageSize", PageSize),
new XAttribute("TotalPages", TotalPageNum),
new XAttribute("Count", TotalNum),
new XAttribute("CurrentPage", CurrentPage),
from d in alldata
select (new XElement(
"vote",
new XElement("id", d.userID),
new XElement("name", d.userRealName),
new XElement("userImg", d.userImg),
new XElement("company", d.userCompany),
new XElement("leaveword", d.userLeaveWord),
new XElement("voteCounts", d.voteCount),
new XElement("img1url", d.caseS5_1),
new XElement("img2url", d.caseS5_2)
))));
context.Response.Write(doc);
context.Response.End();

//---------------------------------------------------

var client = new RestClient
{
BaseUrl = "http://api.douban.com/music/subjects"
};
var request = new RestRequest(Method.GET);
request.AddHeader("User-Agent", Guid.NewGuid().ToString());
request.AddParameter("q", keyWord);
request.AddParameter("start-index", 1);
request.AddParameter("max-results", 20);
request.AddParameter("apikey", "0e63b1707b5f4ccd2b2b1451721de667");
client.ExecuteAsync(request, (response) =>
{
this.MusicList = new ObservableCollection<Music>();
var resource = response.Content;
XElement xmlMusic = XElement.Parse(resource);
var entryCollection = (from f in xmlMusic.Descendants() where f.Name.LocalName == "entry" select f).ToList();
foreach (var entryElement in entryCollection)
{
Music music = new Music();
music.ID = (from f in entryElement.Descendants() where f.Name.LocalName == "id" select f).FirstOrDefault().Value;
music.Title = (from f in entryElement.Descendants() where f.Name.LocalName == "title" select f).FirstOrDefault().Value;
music.Image = new Uri((from f in entryElement.Descendants() where f.Name.LocalName == "link" && f.Attribute("rel").Value == "image" select f.Attribute("href").Value).FirstOrDefault());
music.Pubdate = (from f in entryElement.Descendants() where f.Name.LocalName == "attribute" && f.Attribute("name").Value == "pubdate" select f.Value).FirstOrDefault();
music.Singer = (from f in entryElement.Descendants() where f.Name.LocalName == "attribute" && f.Attribute("name").Value == "singer" select f.Value).FirstOrDefault();
music.Publisher = (from f in entryElement.Descendants() where f.Name.LocalName == "attribute" && f.Attribute("name").Value == "publisher" select f.Value).FirstOrDefault();

MusicRating rating = new MusicRating();
rating.Averate = float.Parse((from f in entryElement.Descendants() where f.Name.LocalName == "rating" select f.Attribute("average").Value).FirstOrDefault());
rating.Max = Int32.Parse((from f in entryElement.Descendants() where f.Name.LocalName == "rating" select f.Attribute("max").Value).FirstOrDefault());
rating.Min = Int32.Parse((from f in entryElement.Descendants() where f.Name.LocalName == "rating" select f.Attribute("min").Value).FirstOrDefault());
rating.NumRaters = Int32.Parse((from f in entryElement.Descendants() where f.Name.LocalName == "rating" select f.Attribute("numRaters").Value).FirstOrDefault());
music.Rating = rating;
this.MusicList.Add(music);
}
this.IsBusy = false;
var url=this.MusicList[0].ID;
this.CreateMusicDeatail(url);
});

原文地址:https://www.cnblogs.com/fx2008/p/3243688.html