×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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

(OP)
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"?>
-<DistanceMatrixResponse> 
   <status>OK</status> 
   <origin_address>Paris, TX, USA</origin_address> 
   <destination_address>Sherman, TX, USA</destination_address> 
  -<row> 
     -<element> 
          <status>OK</status> 
         -<duration> 
              <value>4440</value> 
              <text>1 hour 14 mins</text> 
          </duration> 
         -<distance>
              <value>103031</value> 
              <text>64.0 mi</text> 
          </distance>
      </element>
   </row> 
</DistanceMatrixResponse> 

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()
        {
            InitializeComponent();
        }

        private void btnReadXML_Click(object sender, EventArgs e)
        {
            ReadXMLURL();
        }

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

            while (xmlReader.Read())
            {

            }

        }
    }
} 

Any help with this is much appreciated
Thanks
RJL

RE: C# Windows Form xmlReader

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

CODE

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

Should be a piece of cake.
smile

Cheers,
MakeItSo

“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

(OP)
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();
            xmlDoc.Load(_URLString);

            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