2011-06-08 108 views
0

我在努力阅读这个xml文件作为朋友列表。 我需要的结果作为朋友的列表(List<Friend>)其中朋友是在C中解析xml#

public class Friend 
{ 
    public string UID {get;set;} 
    public string Provider {get;set;} 
    public string PhotoUrl {get;set;} 
    public string ProfileUrl {get;set; 
} 


    <?xml version="1.0" encoding="utf-8"?> 
    <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"> 
     <statusCode>200</statusCode> 
     <errorCode>0</errorCode> 
     <statusReason>OK</statusReason> 
     <callId>ae61ae53a6094364998206a196874d04</callId> 
     <friends> 
     <friend> 
      <UID>_gid_Maj4wFcR3PA10EXENS/SfNhfszDYN9WRQzBgVyOPz0M=</UID> 
      <isSiteUser>false</isSiteUser> 
      <isSiteUID>false</isSiteUID> 
      <identities> 
      <identity> 
       <provider>facebook</provider> 
       <providerUID>100000378470436</providerUID> 
       <isLoginIdentity>false</isLoginIdentity> 
       <nickname>Afzal Raaz</nickname> 
       <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_s.jpg</photoURL> 
       <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_q.jpg</thumbnailURL> 
       <firstName>Afzal</firstName> 
       <lastName>Raaz</lastName> 
       <gender>m</gender> 
       <profileURL>http://www.facebook.com/profile.php?id=100000378470436</profileURL> 
      </identity> 
      </identities> 
      <nickname>Afzal Raaz</nickname> 
      <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_s.jpg</photoURL> 
      <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/70854_100000378470436_113535_q.jpg</thumbnailURL> 
      <firstName>Afzal</firstName> 
      <lastName>Raaz</lastName> 
      <gender>m</gender> 
      <profileURL>http://www.facebook.com/profile.php?id=100000378470436</profileURL> 
     </friend> 
     <friend> 
      <UID>_gid_T6vgh4MDshLvMYzi+Isxa0Ryf0ou2OJf+14pd6iwXlY=</UID> 
      <isSiteUser>false</isSiteUser> 
      <isSiteUID>false</isSiteUID> 
      <identities> 
      <identity> 
       <provider>facebook</provider> 
       <providerUID>100001052246730</providerUID> 
       <isLoginIdentity>false</isLoginIdentity> 
       <nickname>Ajaydeep Singh</nickname> 
       <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_s.jpg</photoURL> 
       <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_q.jpg</thumbnailURL> 
       <firstName>Ajaydeep</firstName> 
       <lastName>Singh</lastName> 
       <gender>m</gender> 
       <profileURL>http://www.facebook.com/profile.php?id=100001052246730</profileURL> 
      </identity> 
      </identities> 
      <nickname>Ajaydeep Singh</nickname> 
      <photoURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_s.jpg</photoURL> 
      <thumbnailURL>https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/203414_100001052246730_126837_q.jpg</thumbnailURL> 
      <firstName>Ajaydeep</firstName> 
      <lastName>Singh</lastName> 
      <gender>m</gender> 
      <profileURL>http://www.facebook.com/profile.php?id=100001052246730</profileURL> 
     </friend> 
     </friends> 
    </socialize.getFriendsInfoResponse> 
+1

还指定您正在使用哪个版本的.NET。如果你能够使用LINQ to XML,它会让你的生活变得更简单。 – 2011-06-08 05:37:02

回答

1

如果你想使用XML序列化,你有两个选择:使用类在System.Xml.Serialization命名空间中,并创建一个类结构,它将保存您需要的数据;或者有一个像xsd.exe这样的工具来为你生成类。使用前者可以获得简明的数据模型,后者可能会获得比您真正需要的更多的类(自动生成的价格)。

如果你想使用XSD.EXE,你会先救你的XML文件中的(说“file.xml”),然后运行它来创建该文件的架构:

xsd.exe file.xml 

这将创建一个名为file.xsd的文件,其中包含该XML的架构。然后你XSD.EXE再次运行产生可由XmlSerializer的可用于消费的是XML类:

xsd.exe /c file.xsd 
+0

是很好的帮助解决问题。 – Parminder 2011-06-08 06:31:59

0
public class FriendList 
    { 
     public List<Friend> friends; 
     public FriendList() 
     { 
     friends= new List<Friend>(); 
     } 
    } 

public class Friend 
{ 
    public string UID {get;set;} 
    public string Provider {get;set;} 
    public string PhotoUrl {get;set;} 
    public string ProfileUrl {get;set;} 
} 

Public class ParseFriends 
{ 
    FriendList p = new FriendList(); 
    public ReadFriends() 
    { 
    DirectoryInfo dir = new DirectoryInfo("../path to your xml file"); 
    FileInfo[] files = dir.GetFiles("*.*"); // read all xml file from a folder 
    XmlDocument doc = new XmlDocument(); 

    foreach (FileInfo f in files) 
      { 
       Friend e = new Friend(); 
       doc.Load(f.FullName); 
       e.UID = doc.GetElementsByTagName("UID")[0].InnerText; 
       e.Provider = doc.GetElementsByTagName("Provider")[0].InnerText; 
       e.PhotoUrl = doc.GetElementsByTagName("PhotoUrl")[0].InnerText; 
       e.ProfileUrl = doc.GetElementsByTagName("ProfileUrl")[0].InnerText; 
       p.empDetails.Add(e); 
      } 
      return p; 
    } 
} 

尝试......

+0

你在说什么pranay。我需要从这个XML文件中得到一个朋友列表。 – Parminder 2011-06-08 05:35:49

+0

此代码中缺少的东西... ReadFriends没有做任何有用的... – carlosfigueira 2011-06-08 05:36:02

+0

感谢编辑,它现在做了一些:) – carlosfigueira 2011-06-08 15:13:12

0

你可以尝试这样的事情。

XDocument doc = XDocument.Parse(@" ... XML ... "); 
Func<XElement, string, string> get = 
    (el, name) => (string)el.Element(XName.Get(name, "urn:com:gigya:api")); 
var friends = 
    from el in doc.Descendants(XName.Get("friend", "urn:com:gigya:api")) 
    select new Friend 
     { 
      UID = get(el, "UID"), 
      PhotoUrl = get(el, "photoURL"), 
      ProfileUrl = get(el, "profileURL"), 
     }; 
List<Friend> friendList = friends.ToList();