Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

C# Windows Form xmlReader

C# Windows Form xmlReader

C# Windows Form xmlReader

I am new to C# and trying to learn how to best get a particular value from and XML. I have the following URL (http://maps.googleapis.com/maps/api/distancematrix...) that returns and XML document with the distance between two cities (using Google Maps API)

This is the XML the URL generates. I need to get just the distance (highlighted in yellow)

CODE --> xml

<?xml version="1.0" encoding="UTF-8"?>
   <origin_address>Paris, TX, USA</origin_address> 
   <destination_address>Sherman, TX, USA</destination_address> 
              <text>1 hour 14 mins</text> 
              <text>64.0 mi</text> 

Here is what I have so far just stuck on how to get to the specific element and how to assign it to a variable so I can use it later on

CODE --> c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Test
    public partial class Form1 : Form
        string _URLString = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Paris+TX&destinations=Sherman+TX&mode=driving&language=en-EN&sensor=false&units=imperial";
        public Form1()

        private void btnReadXML_Click(object sender, EventArgs e)

        private void ReadXMLURL()
            XmlTextReader xmlReader = new XmlTextReader(_URLString);
            xmlReader.WhitespaceHandling = WhitespaceHandling.Significant; 

            while (xmlReader.Read())



Any help with this is much appreciated

RE: C# Windows Form xmlReader

I'd recommend using an XMLDocument rather than a Reader.
Then you can access nodes directly via XPath:


String distance = xmldoc.SelectSingleNode("//distance/text").InnerText 

Should be a piece of cake.


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: C# Windows Form xmlReader

Like a charm. Thanks so much here is the final code

CODE --> c#

private void ReadXMLURL()
            string _URLString = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + _From + "&destinations=" + _To + "&mode=driving&language=en-EN&sensor=false&units=imperial";

            XmlDocument xmlDoc = new XmlDocument();

            String distance = xmlDoc.SelectSingleNode("//distance/text").InnerText;
            txtDistance.Text = distance;


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close