如何从C#中的Google Contacts API解析xml响应

时间:2012-07-09 14:52:10

标签: parsing google-api google-contacts

我正在尝试通过C#应用程序中的Google Contacts API获取联系人列表。

以下是我用于提取Google通讯录的代码,但在解析响应时不确定:

        OAuthGContacts _google = new OAuthGContacts();
        if (Request["code"] == null)
        {
            Response.Redirect(_google.AuthorizationLinkGet());

        }
        else
        {
              //Get the access token and secret.
            _google.AccessTokenGet(Request["code"]);

            if (_google.Token.Length > 0)
            {
             string   _contactResponse = _google.WebRequest(OAuthGContacts.Method.GET, "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + _google.Token, string.Empty);
             XmlReader reader = XmlReader.Create(new StringReader(_contactResponse ));
             SyndicationFeed feed = SyndicationFeed.Load(reader);
             reader.Close();
            }
        }

请建议如何解析回复以提取Google通讯录。

1 个答案:

答案 0 :(得分:1)

您无需解析任何XML响应即可在C#应用程序中获取联系人列表。 Google的Data API提供了执行此操作的功能。 Here是一个.NET示例。如果您看到协议,只需单击.NET选项卡,您将看到以下代码

public static void PrintAllContacts(ContactsRequest cr)
{
  Feed<Contact> f = cr.GetContacts();
  foreach (Contact entry in f.Entries)
  ....