C# LINQ + XML XElement + WCF Rest Services 方法

C# LINQ + XML XElement + WCF Rest Services 方法

   WS.cs ( Rest Client )
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Xml.Linq;
 
   private List GetAttribute(string xml)
   {
 
      XElement elem = XElement.Load(xml);
 
      XNamespace ns = "http://schemas.datacontract.org/2004/07/SoccerRest";
 
      List list = (from p in elem.Descendants(ns + "PLAYER_VIEW")
                        select new player
                        {
                           Age = (int)p.Element(ns + "Age"),
                           PlayerName = p.Element(ns + "Player").ToString(),
                           Nation = p.Element(ns + "Nation").ToString(),
                           Position = p.Element(ns + "Position").ToString(),
                           Team = p.Element(ns + "Team").ToString(),
                           PlayerID = (int)p.Element(ns + "PlayerID")
                        }).ToList();
 
      return list;
 
   }
 
   Player.cs ( Entity )
 
   public class player
   {
      public int Age { get; set; }
      public String Nation { get; set; }
      public String PlayerName { get; set; }
      public int PlayerID { get; set; }
      public String Position { get; set; }
      public String Team { get; set; }
   }
 
   Rest Format ( XML Format )
 
   <ArrayOfPLAYER_VIEW xmlns="http://schemas.datacontract.org/2004/07/SoccerRest">
 
   …
 
   <PLAYER_VIEW>
      <Age>25</Age>
      <Nation>Germany</Nation>
      <Player>Mesut Özil</Player>
      <PlayerID>1</PlayerID>
      <Position>Midfielders</Position>
      <Team>Arsenal F.C.</Team>
   </PLAYER_VIEW>
 
   <PLAYER_VIEW>
      <Age>29</Age>
      <Nation>Portugal</Nation>
      <Player>Cristiano Ronaldo</Player>
      <PlayerID>2</PlayerID>
      <Position>Forwards</Position>
      <Team>Real Madrid C.F.</Team>
   </PLAYER_VIEW>
 
   …
 
   </ArrayOfPLAYER_VIEW>