Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:17:21+00:00 2026-06-01T09:17:21+00:00

im trying to parse the XML returned by Google contacts API i made some

  • 0

im trying to parse the XML returned by Google contacts API

i made some helper classes to give me strongly-typed access to the necessary data, but cant seem to be making them work in tandem.

i made a class GoogleDocument which inherits XDocument and a GoogleContact which inherits XElement

Class GoogleDocument
Inherits XDocument
Dim xnsAtom = XNamespace.Get("http://www.w3.org/2005/Atom")
Sub New()
    MyBase.new()
End Sub
Sub New(other As XDocument)
    MyBase.New(other)
End Sub
ReadOnly Property Entries As IEnumerable(Of GoogleContact)
    Get
        Dim feed = Element(xnsAtom + "feed")
        Dim ret = New List(Of GoogleContact)
        For Each e In feed.Elements(xnsAtom + "entry")
            ret.Add(New GoogleContact(e))
        Next
        Return ret.AsEnumerable
    End Get
End Property
End Class

Class GoogleContact
Inherits XElement
Dim xnsGd = XNamespace.Get("http://schemas.google.com/g/2005")
Dim xnsAtom = XNamespace.Get("http://www.w3.org/2005/Atom")
Dim xnsApp = XNamespace.Get("http://www.w3.org/2007/app")
Sub New(other As XElement)
    MyBase.new(other)
End Sub
ReadOnly Property ETag As String
    Get
        Return Attribute(xnsGd + "etag").Value
    End Get
End Property
ReadOnly Property ContactID As Integer
    Get
        Dim uri = Element(xnsAtom + "id").Value
        Return uri.Substring(uri.LastIndexOf("/") + 1)
    End Get
End Property
ReadOnly Property Edited As DateTime
    Get
        Return Date.Parse(Element(xnsApp + "edited").Value)
    End Get
End Property
End Class

questions:

  1. isn’t there an easier way to convert all matching elements to GoogleContacts? then adding each 1 via iteration. also it seems that GoogleContact is not really an XElement as in the debugger it shows up as {<entry....>} instead of <entry....> im not sure what these braces mean here, but its odd
  2. why do i need to declare the namespaces again and again? isn’t there a way to somehow pass over to GoogleContact all relevant namespaces? the way its now, Google refuses to accept the data back as all namespaces become “p1”

i’d appreciate any advice on the subject

thank you very much

EDIT

here’s a more complete code sample, with changes per Jon Skeet’s suggestions

Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Collections.Specialized
Imports System.Runtime.CompilerServices

Module Module1

Dim GUserName As String
Dim GPassword As String
Sub Main()
    Dim authRequest As HttpWebRequest = HttpWebRequest.Create("https://www.google.com/accounts/ClientLogin")
    authRequest.KeepAlive = True
    authRequest.ContentType = "application/x-www-form-urlencoded"
    authRequest.Method = "POST"
    Dim encoder = New ASCIIEncoding
    Dim encodedData = encoder.GetBytes("Email=" & GUserName & "&Passwd=" & GPassword & "&source=Consultor&service=cp&accountType=HOSTED_OR_GOOGLE")
    authRequest.ContentLength = encodedData.Length
    Dim requestStream = authRequest.GetRequestStream
    requestStream.Write(encodedData, 0, encodedData.Length)
    requestStream.Close()
    Dim authResponse = authRequest.GetResponse
    Dim readStream = New StreamReader(authResponse.GetResponseStream, encoder)
    Dim body = readStream.ReadToEnd
    Dim tokens = TextCollection(body, "=", Chr(10))
    Dim req2 = New GoogleClient(tokens("auth"))
    body = req2.GetString("default/full?max-results=5000")
    Dim gDoc = New GoogleDocument(XDocument.Parse(body))
    Dim dcx = DBEntities()
    Dim pers = dcx.Persons
    For Each ge In gDoc.Entries
        Dim entry = ge
        Dim id As String = entry.ContactID
        Dim p As Object '= (From x In pers Where x.GoogleCode = id).FirstOrDefault' cant ompile iin this demo
        If p Is Nothing Then Exit For
        If entry.Edited > p.LastEdit Then
            p.GoogleCode = entry.ContactID
            dcx.SaveChanges()
        Else
            Dim updClient = New GoogleClient(tokens("auth"))
            updClient.ETag = entry.ETag
            Dim updResp = updClient.PutString("http://www.google.com/m8/feeds/contacts/" & GUserName & "/base/" & entry.ContactID, entry.UpdateXml)
        End If
    Next
End Sub
Class GoogleClient
    Inherits WebClient
    Property ETag As String

    Const UrlStart = "https://www.google.com/m8/feeds/contacts/"
    Sub New(AuthToken As String)
        Headers.Add("Content-Type", "application/atom+xml; charset=UTF-8")
        Headers.Add("User-Agent", "G-Consultor/GDataGAuthRequestFactory-CS-Version=1.9.0.23118--IEnumerable")
        Headers.Add("Authorization", "GoogleLogin auth=" & AuthToken)
        Headers.Add("GData-Version", "3.0")
    End Sub
    Function GetString(Path As String) As String
        Return DownloadString(UrlStart & Path)
    End Function
    Public Function PutString(address As String, data As String) As String
        If ETag <> "" Then
            Headers.Add("Etag", ETag)
            Headers.Add("If-Match", ETag)
        End If
        Return UploadString(address, "PUT", data)
    End Function
End Class
Function TextCollection(Text As String, FieldDelimiter As String, Optional RowDelimiter As String = vbCrLf) As NameValueCollection
    Text = Text.RightCut(RowDelimiter)
    Dim ret = New NameValueCollection
    Dim rows = Text.Split(RowDelimiter)
    For Each cl In rows
        ret.Add(cl.Substring(0, cl.IndexOf(FieldDelimiter)), cl.Substring(cl.IndexOf(FieldDelimiter) + FieldDelimiter.Length))
    Next
    Return ret
End Function
Class GoogleDocument
    Inherits XDocument
    Dim xnsAtom = XNamespace.Get("http://www.w3.org/2005/Atom")
    Sub New()
        MyBase.new()
    End Sub
    Sub New(other As XDocument)
        MyBase.New(other)
    End Sub
    ReadOnly Property Entries As IEnumerable(Of GoogleContact)
        Get
            Dim feed = Element(xnsAtom + "feed")
            Dim ret = New List(Of GoogleContact)
            For Each e In feed.Elements(xnsAtom + "entry")
                ret.Add(New GoogleContact(e))
            Next
            Return ret.AsEnumerable
        End Get
    End Property
End Class
Function DBEntities() As Object 'really should return my EF data model
    Return Nothing
End Function
<Extension()> Function RightCut(value As String, CutString As String) As String
    If Right(value, CutString.Length) = CutString Then value = value.Substring(0, value.Length - CutString.Length)
    Return value
End Function
Class GoogleContact
    Dim xnsGd = XNamespace.Get("http://schemas.google.com/g/2005")
    Dim xnsAtom = XNamespace.Get("http://www.w3.org/2005/Atom")
    Dim xnsApp = XNamespace.Get("http://www.w3.org/2007/app")
    Dim xContact As XElement
    Sub New(entry As XElement)
        xContact = entry
    End Sub
    ReadOnly Property ETag As String
        Get
            Return xContact.Attribute(xnsGd + "etag").Value
        End Get
    End Property
    ReadOnly Property ContactID As Integer
        Get
            Dim uri = xContact.Element(xnsAtom + "id").Value
            Return uri.Substring(uri.LastIndexOf("/") + 1)
        End Get
    End Property
    ReadOnly Property Edited As DateTime
        Get
            Return Date.Parse(xContact.Element(xnsApp + "edited").Value)
        End Get
    End Property

    ReadOnly Property UpdateXml
        Get
            Return "<?xml version=""1.0"" encoding=""utf-8""?>" & xContact.ToString
        End Get
    End Property

    Overrides Function ToString() As String
        Return xContact.ToString
    End Function
End Class
End Module
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:17:23+00:00Added an answer on June 1, 2026 at 9:17 am

    It’s still not entirely clear what’s actually going wrong – if you specify the right namespace everywhere, it should all be fine. However, you don’t need to repeat the names all over the place – you may well want to create shared readonly fields of type XName to avoid typos. I’d also use the implicit conversion from String to XName for simplicity. In C# I’d write this as something like:

    private static readonly XNamespace AtomNs = "http://www.w3.org/2005/Atom";
    private static readonly XNamespace GoogleDataNs =
        "http://schemas.google.com/g/2005";
    private static readonly XNamespace AppNs = "http://www.w3.org/2007/app";
    
    // You should work out where to put these, and their visibility
    public static readonly XName FeedElementName = AtomNs + "feed";
    public static readonly XName EntryElementName = AtomNs + "entry";
    public static readonly XName ETagAttributeName = GoogleDataNs + "etag";
    // etc
    

    Then you can use those “constants” everywhere – it doesn’t matter that they’ve got namespaces; that doesn’t end up getting used within your code, because you’re just referring to “the element name” or “the attribute name” appropriately.

    In general terms, ways to improve your code:

    • I’d still suggest using the .NET API if you can; you say you “didn’t find them very helpful” but that may just mean that you didn’t investigate them for long enough. Once you’ve got the hang of them, they’re likely to save you time.
    • I wouldn’t use any inheritance in the code you’ve given – there’s no need to derive from WebClient, XDocument or XElement. Your code will be clearer without the inheritance here, as you can create just the members which are relevant for the object you’re try to model.
    • You can use LINQ to query the XDocument and create a collection of contacts. For example (C# again, but VB would be similar):

      List<GoogleContact> = document.Root
                 .Elements(GoogleContact.EntryElementName)
                 .Select(element => new GoogleContact(element))
                 .ToList();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to parse the XML returned by the Google Geo code API ,but
I'm trying to parse some xml using simplexml and php...it's being returned to me
I'm trying to parse XML returned from the Youtue API. The APIcalls work correctly
Trying to parse some XML but apparently this is too much for a lazy
I am trying to parse some XML i have retrieved via e4x in an
I am trying to parse some XML, however, I get the error above this
I am trying to parse one XML file that contains some unicode characters.I tried
I am trying to parse the XML returned by search engine APIs (Bing, Yahoo
I ran into a problem while trying to parse an XML string returned from
I am trying to use XDocument.Parse(string s) to parse some XML that is being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.