0

I am struggling to read this xml file as list of friends. I need the result as List of friend (List<Friend>) where Friend is

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
  • 1
    Also specify which version of .NET you're using. If you're able to use LINQ to XML, it'll make your life simpler. Commented Jun 8, 2011 at 5:37

3 Answers 3

1

If you want to use XML serialization, you have two options: use the classes in the System.Xml.Serialization namespace, and create a class structure which will hold the data you need; or have a tool such as xsd.exe to generate the classes for you. Using the former you'll get a concise data model, with the latter you may get more classes than you really need (the price for auto-generation).

If you want to use xsd.exe, you'd first save your XML in a file (say "file.xml"), then run it to create the schema for the file:

xsd.exe file.xml

This will create a file called file.xsd which contains the schema for that XML. Then you run xsd.exe again to generate the classes which can be used by the XmlSerializer to consume that XML:

xsd.exe /c file.xsd
Sign up to request clarification or add additional context in comments.

Comments

0

You can try something like this.

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();

Comments

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;
  }
}

Try this...

1 Comment

what are you saying pranay. I need to get a list of friends from this xml file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.