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)
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
Any help with this is much appreciated
Thanks
RJL
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