2010-06-13 2 views
1

LINQ 2 XML을 사용하여 Google Contacts API를 통해 XML 피드를 구문 분석하려고합니다. 그 피드는 다음과 같습니다.linq to xml을 사용하여 Google 피드 구문 분석

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005"> 
    <id>[email protected]</id> 
    <updated>2010-06-11T17:37:06.561Z</updated> 
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" /> 
    <title type="text">My Contacts</title> 
    <link rel="alternate" type="text/html" href="http://www.google.com/" /> 
    <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/full" /> 
    <link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/full" /> 
    <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/full/batch" /> 
    <link rel="self" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/full?max-results=25" /> 
    <author> 
    <name>My NAme</name> 
    <email>[email protected]</email> 
    </author> 
    <generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator> 
    <openSearch:totalResults>19</openSearch:totalResults> 
    <openSearch:startIndex>1</openSearch:startIndex> 
    <openSearch:itemsPerPage>25</openSearch:itemsPerPage> 
    <entry> 
    <id>http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/base/0</id> 
    <updated>2010-01-26T20:34:03.802Z</updated> 
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" /> 
    <title type="text">Contact name</title> 
    <link rel="http://schemas.google.com/contacts/2008/rel#edit-photo" type="image/*" href="http://www.google.com/m8/feeds/photos/media/myemailaddress%40gmail.com/0/O-ydnzWMJcfZWqT-6gGetw" /> 
    <link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="http://www.google.com/m8/feeds/photos/media/myemailaddress%40gmail.com/0" /> 
    <link rel="self" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/full/0" /> 
    <link rel="edit" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/myemailaddress%40gmail.com/full/0/1264538043802000" /> 
    <gd:email rel="http://schemas.google.com/g/2005#other" address="[email protected]" primary="true" /> 
    </entry> 
</feed> 

저는 linq 2 sql을 사용하여 많은 작업을 시도했지만 작동하지 않았습니다. 이 간단한 코드를 잘라내어도 작동하지 않습니다.

using (FileStream stream = File.OpenRead("response.xml")) 
{ 

    XmlReader reader = XmlReader.Create(stream); 

    XDocument doc = XDocument.Load(reader); 

    XElement feed = doc.Element("feed"); 
    if (feed == null) 
    { 
     Console.WriteLine("feed not found"); 
    } 

    XElement id = doc.Element("id"); 
    if (id == null) 
    { 
     Console.WriteLine("id is null"); 
    } 
} 

문제는 id와 feed가 모두 null입니다. 내가 뭘 잘못 했니?

답변

2

당신은 당신의 XName이있는 요소의 네임 스페이스를 지정해야합니다 :

  XElement feed = doc.Element("{http://www.w3.org/2005/Atom}feed"); 
      if (feed == null) 
      { 
       Console.WriteLine("feed not found"); 
      } 
0

당신은 그것에 대해 LINQ to XML을 필요하지 않습니다 : 당신이 SyndicationFeed 클래스 대신

1
<?php 
    $token_key = $this->params['url']['token']; 
    $google_contacts_xml_feed = simplexml_load_file('https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$token_key);//'.$id); 

    $gmail_name = $google_contacts_xml_feed->author->name; 
    $gmail_id = $google_contacts_xml_feed->author->email; 
    echo $gmail_id; 
    echo $gmail_name; 
?> 
을 사용할 수 있습니다
관련 문제