Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Windows Form xmlReader

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
I am new to C# and trying to learn how to best get a particular value from and XML. I have the following URL ( 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)

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>[b][highlight #FCE94F]64.0 mi[/highlight][/b]</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:
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 = @"[URL unfurl="true"]http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Paris+TX&destinations=Sherman+TX&mode=driving&language=en-EN&sensor=false&units=imperial";[/URL]
        
        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
 
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.
:)

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.
 
Like a charm. Thanks so much here is the final code

Code:
        private void ReadXMLURL()
        {
            string _URLString = @"[URL unfurl="true"]http://maps.googleapis.com/maps/api/distancematrix/xml?origins="[/URL] + _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;

        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top