Q:

Google Analytics API exception in C#

I am new to Google Analytics and tried to access analytics data with an example I found online.
It throws the below exception:
Google.GData.Client.ClientFeedException: Parsing failed ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
  at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
  at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
  at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
  at Google.GData.Client.BaseFeedParser.MoveToStartElement(XmlReader reader)
  at Google.GData.Client.AtomFeedParser.Parse(Stream streamInput, AtomFeed feed)
  --- End of inner exception stack trace ---
  at Google.GData.Client.AtomFeedParser.Parse(Stream streamInput, AtomFeed feed)
  at Google.GData.Client.AtomFeed.Parse(Stream stream, AlternativeFormat format)
  at Google.GData.Client.Service.CreateAndParseFeed(Stream inputStream, Uri uriToUse)
  at Google.GData.Client.Service.Query(FeedQuery feedQuery)
  at Google.GData.Analytics.AnalyticsService.Query(DataQuery feedQuery)
  at GoogleAnalytics.Program.Main(String[] args) in e:\\Code\\GoogleAnalytics\\GoogleAnalytics\\Program.cs:line 40

Below is the code. Can someone help me if possible? I couldn't find proper documentation on how to do it. Thanks in advance your help.
string username = "xxx@gmail.com";
string pass = "Password";
string gkey = "?key=<<key>>";

string dataFeedUrl = "https://www.googleapis.com/auth/analytics.readonly/" + gkey;

AnalyticsService service = new AnalyticsService("WebApp");
service.setUserCredentials(username, pass);

DataQuery query1 = new DataQuery(dataFeedUrl);

query1.Ids = "ga:1235466";
query1.Metrics = "ga:visits";
query1.Sort = "ga:visits";
query1.Dimensions = "ga:dimension1";

query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd");
query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
query1.StartIndex = 1;

DataFeed dataFeedVisits = service.Query(query1);

foreach (DataEntry entry in dataFeedVisits.Entries)
{
    string st = entry.Title.Text;
    string ss = entry.Metrics[0].Value;                   
}

A:

To start I would like to recommend you use the Google.Apis.v3 libs. They make your life a lot easer.
You should be using Oauth2 to authenticate access not a username and password.   After you have created your application in Google Apis Console You receive a clientid and a client secret these identify your application to Google.   The oauth2 code looks something like this.
UserCredential credential; 
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
          new ClientSecrets { ClientId = "YourClientId", ClientSecret = "YourClientSecret" },
          new[] { AnalyticsService.Scope.AnalyticsReadonly },
          "user",
          CancellationToken.None,
          new FileDataStore("Drive.Auth.Store")).Result; }

The code above will ask the user if they want to allow you access to there Google Analtyics Data. 
Once you have the UserCredential's you can then create the service you will use for all of your requests to the API.
AnalyticsService service = new AnalyticsService(
                           new BaseClientService.Initializer() {
                           HttpClientInitializer = credential,
                           ApplicationName = "Analytics API sample", });

You can find a tutorial on most of the Calls to the Google Analtyics api here

Note: If your only accessing your own data you should check out Service Accounts.  

