Reputation: 2109
I have the below piece of code that accepts an XMLDocument object and a xml of the format mentioned below the code. I want to read the values of 4 tags present in the bouding box:
SouthLatitude, etc
public static void readXML(XmlDocument document)
{
if (document == null)
{
throw new ArgumentNullException("document");
}
XmlNode specificNode = document.SelectSingleNode("/Response/ResourceSets/ResourceSet/Resources/Location/BoundingBox");
if (specificNode != null)
{
XmlNodeReader specificNodeReader = new XmlNodeReader(specificNode);
while (specificNodeReader.Read())
{
Console.WriteLine(specificNodeReader.Value);
}
}
}
The xml looks like this:
<?xml version="1.0" encoding="utf-8" ?>
- <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<Copyright>Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
<BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<TraceId>f651e16fe1204e12b848084d73f5148d|SINM001008|02.00.83.1900|SINMSNVM001115, SINMSNVM001124</TraceId>
- <ResourceSets>
- <ResourceSet>
<EstimatedTotal>1</EstimatedTotal>
- <Resources>
- <Location>
<Name><Some Name</Name>
- <Point>
<Latitude>47.640570402145386</Latitude>
<Longitude>-122.12937377393246</Longitude>
</Point>
- <BoundingBox>
<SouthLatitude>47.636707684574709</SouthLatitude>
<WestLongitude>-122.13701709146854</WestLongitude>
<NorthLatitude>47.644433119716062</NorthLatitude>
<EastLongitude>-122.12173045639638</EastLongitude>
</BoundingBox>
<EntityType>Address</EntityType>
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
Can someone point out, what I am missing here?
Upvotes: 0
Views: 520
Reputation: 1576
XmlNode specificNode = document.SelectSingleNode("/Response/ResourceSets/ResourceSet/Resources/Location/BoundingBox");
if (specificNode != null)
{
foreach (XmlNode child in specificNode.ChildNodes)
{
Console.WriteLine(child.InnerText);
}
}
Should do the trick. Of course a lot of credit goes to Henk Holterman in his earlier answer.
Upvotes: 0
Reputation: 107327
Because your actual xml
has a namespace, you will need to include it in any Xpath
queries you use. Since you already have the xml loaded in an XmlDocument
, you don't need an XmlReader
XmlNamespaceManager nsm = new XmlNamespaceManager(document.NameTable);
nsm.AddNamespace("ms", "http://schemas.microsoft.com/search/local/ws/rest/v1");
XmlNode boundingBoxNode = document.SelectSingleNode("/ms:Response/ms:ResourceSets/ms:ResourceSet/ms:Resources/ms:Location/ms:BoundingBox", nsm);
if (boundingBoxNode != null)
{
Console.WriteLine(boundingBoxNode.SelectSingleNode("ms:SouthLatitude", nsm).InnerText);
Console.WriteLine(boundingBoxNode.SelectSingleNode("ms:NorthLatitude", nsm).InnerText);
Console.WriteLine(boundingBoxNode.SelectSingleNode("ms:EastLongitude", nsm).InnerText);
Console.WriteLine(boundingBoxNode.SelectSingleNode("ms:WestLongitude", nsm).InnerText);
}
You can also do this in Linq to Xml:
XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
var boundingBox = document
.Descendants(ns + "Response")
.Descendants(ns + "ResourceSets")
.Descendants(ns + "ResourceSet")
.Descendants(ns + "Resources")
.Descendants(ns + "Location")
.Descendants(ns + "BoundingBox");
if (boundingBox != null)
{
Console.WriteLine(boundingBox.Descendants(ns + "SouthLatitude").First().Value);
Console.WriteLine(boundingBox.Descendants(ns + "NorthLatitude").First().Value);
Console.WriteLine(boundingBox.Descendants(ns + "EastLongitude").First().Value);
Console.WriteLine(boundingBox.Descendants(ns + "WestLongitude").First().Value);
}
Upvotes: 2