简体   繁体   中英

find specific nodes in Microsoft REST XML response

i tried to find the the value in the node TravelDistance,as it show in the picture but i also get Null this is my try

string url = @"https://dev.virtualearth.net/REST/V1/Routes?wp.0=43.104234, -77.627686&wp.1=43.103219, -77.621267&wp.2=43.100619, -77.624963&output=xml&distanceUnit=Mile&key= Ap5gFiZ56mvoqUXoswD2Hw81EZk5xMaCcP6JNzJOc-0xAf2wqTYgvSUZ8uhXJifZ";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(url);

            XmlNode nodea = xmlDoc.SelectSingleNode("/Response/ResourceSets/ResourceSet/Resources/Route/TravelDistance");

在此处输入图片说明

XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace("x", "http://schemas.microsoft.com/search/local/ws/rest/v1");
var dist = xmlDoc.SelectSingleNode("//x:Route/x:TravelDistance", mgr).InnerText;

problem solved by this way

string url = @"https://dev.virtualearth.net/REST/V1/Routes?wp.0=43.104234, -77.627686&wp.1=43.103219, -77.621267&wp.2=43.100619, -77.624963&output=xml&distanceUnit=Mile&key= Ap5gFiZ56mvoqUXoswD2Hw81EZk5xMaCcP6JNzJOc-0xAf2wqTYgvSUZ8uhXJifZ";

            HttpWebRequest req = WebRequest.Create(url)
                                  as HttpWebRequest;
            string result = null;
            using (HttpWebResponse resp = req.GetResponse()  as HttpWebResponse)
            {
                StreamReader reader =
                    new StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
              string TravelDistance = result.Substring(result.IndexOf("<TravelDistance>") + "<TravelDistance>".Length, (result.IndexOf("</TravelDistance>") - result.IndexOf("<TravelDistance>")) - "<TravelDistance>".Length);

if any one have addition solution tell as please

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM