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)
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)
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
Then you can access nodes directly via XPath:
CODE
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.
RE: C# Windows Form xmlReader
CODE --> c#