Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 5.08 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Parsing xml in C#
  2. public class Friend
  3. {
  4.     public string UID {get;set;}
  5.     public string Provider {get;set;}
  6.     public string PhotoUrl {get;set;}
  7.     public string ProfileUrl {get;set;
  8. }
  9.  
  10.  
  11.     <?xml version="1.0" encoding="utf-8"?>
  12.     <socialize.getFriendsInfoResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:com:gigya:api http://socialize-api.gigya.com/schema" xmlns="urn:com:gigya:api">
  13.       <statusCode>200</statusCode>
  14.       <errorCode>0</errorCode>
  15.       <statusReason>OK</statusReason>
  16.       <callId>ae61ae53a6094364998206a196874d04</callId>
  17.       <friends>
  18.         <friend>
  19.           <UID>_gid_Maj4wFcR3PA10EXENS/SfNhfszDYN9WRQzBgVyOPz0M=</UID>
  20.           <isSiteUser>false</isSiteUser>
  21.           <isSiteUID>false</isSiteUID>
  22.           <identities>
  23.             <identity>
  24.               <provider>facebook</provider>
  25.               <providerUID>100000378470436</providerUID>
  26.               <isLoginIdentity>false</isLoginIdentity>
  27.               <nickname>Afzal Raaz</nickname>
  28.               <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_s.jpg</photoURL>
  29.               <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_q.jpg</thumbnailURL>
  30.               <firstName>Afzal</firstName>
  31.               <lastName>Raaz</lastName>
  32.               <gender>m</gender>
  33.               <profileURL>http://www.facebook.com/profile.php?id=100000378470436</profileURL>
  34.             </identity>
  35.           </identities>
  36.           <nickname>Afzal Raaz</nickname>
  37.           <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_s.jpg</photoURL>
  38.           <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_q.jpg</thumbnailURL>
  39.           <firstName>Afzal</firstName>
  40.           <lastName>Raaz</lastName>
  41.           <gender>m</gender>
  42.           <profileURL>http://www.facebook.com/profile.php?id=100000378470436</profileURL>
  43.         </friend>
  44.         <friend>
  45.           <UID>_gid_T6vgh4MDshLvMYzi+Isxa0Ryf0ou2OJf+14pd6iwXlY=</UID>
  46.           <isSiteUser>false</isSiteUser>
  47.           <isSiteUID>false</isSiteUID>
  48.           <identities>
  49.             <identity>
  50.               <provider>facebook</provider>
  51.               <providerUID>100001052246730</providerUID>
  52.               <isLoginIdentity>false</isLoginIdentity>
  53.               <nickname>Ajaydeep Singh</nickname>
  54.               <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_s.jpg</photoURL>
  55.               <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_q.jpg</thumbnailURL>
  56.               <firstName>Ajaydeep</firstName>
  57.               <lastName>Singh</lastName>
  58.               <gender>m</gender>
  59.               <profileURL>http://www.facebook.com/profile.php?id=100001052246730</profileURL>
  60.             </identity>
  61.           </identities>
  62.           <nickname>Ajaydeep Singh</nickname>
  63.           <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_s.jpg</photoURL>
  64.           <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_q.jpg</thumbnailURL>
  65.           <firstName>Ajaydeep</firstName>
  66.           <lastName>Singh</lastName>
  67.           <gender>m</gender>
  68.           <profileURL>http://www.facebook.com/profile.php?id=100001052246730</profileURL>
  69.         </friend>
  70.       </friends>
  71.     </socialize.getFriendsInfoResponse>
  72.        
  73. xsd.exe file.xml
  74.        
  75. xsd.exe /c file.xsd
  76.        
  77. XDocument doc = XDocument.Parse(@" ... XML ... ");
  78. Func<XElement, string, string> get =
  79.     (el, name) => (string)el.Element(XName.Get(name, "urn:com:gigya:api"));
  80. var friends =
  81.     from el in doc.Descendants(XName.Get("friend", "urn:com:gigya:api"))
  82.     select new Friend
  83.         {
  84.             UID = get(el, "UID"),
  85.             PhotoUrl = get(el, "photoURL"),
  86.             ProfileUrl = get(el, "profileURL"),
  87.         };
  88. List<Friend> friendList = friends.ToList();
  89.        
  90. public class FriendList
  91.     {
  92.       public List<Friend> friends;
  93.       public FriendList()
  94.       {
  95.         friends= new List<Friend>();
  96.       }
  97.     }
  98.  
  99. public class Friend
  100. {  
  101.   public string UID {get;set;}
  102.   public string Provider {get;set;}  
  103.   public string PhotoUrl {get;set;}  
  104.   public string ProfileUrl {get;set;}
  105. }
  106.  
  107. Public class ParseFriends
  108. {
  109.   FriendList p = new FriendList();
  110.   public ReadFriends()
  111.   {
  112.     DirectoryInfo dir = new DirectoryInfo("../path to your xml file");
  113.     FileInfo[] files = dir.GetFiles("*.*"); // read all xml file from a folder
  114.     XmlDocument doc = new XmlDocument();
  115.  
  116.     foreach (FileInfo f in files)
  117.             {
  118.                 Friend e = new Friend();
  119.                 doc.Load(f.FullName);
  120.                 e.UID = doc.GetElementsByTagName("UID")[0].InnerText;
  121.                 e.Provider = doc.GetElementsByTagName("Provider")[0].InnerText;
  122.                 e.PhotoUrl = doc.GetElementsByTagName("PhotoUrl")[0].InnerText;
  123.                 e.ProfileUrl = doc.GetElementsByTagName("ProfileUrl")[0].InnerText;
  124.                 p.empDetails.Add(e);
  125.             }
  126.             return p;
  127.   }
  128. }