0

我有下面的一段代码,它接受一个 XMLDocument 对象和代码下面提到的格式的 xml。我想读取边界框中存在的 4 个标签的值:

南纬等

    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);
            }
        }
     }

xml 看起来像这样:

      <?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>

有人可以指出,我在这里缺少什么?

4

2 回答 2

2

因为您的实际xml有一个命名空间,所以您需要将它包含在Xpath您使用的任何查询中。由于您已经在 中加载了 xml XmlDocument,因此您不需要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);
}        

您也可以在 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);
}        
于 2012-12-20T12:50:19.367 回答
0
        XmlNode specificNode = document.SelectSingleNode("/Response/ResourceSets/ResourceSet/Resources/Location/BoundingBox");
        if (specificNode != null)
        {
            foreach (XmlNode child in specificNode.ChildNodes)
            {
                Console.WriteLine(child.InnerText);
            }
        }

应该做的伎俩。当然,很多功劳归功于 Henk Holterman 在他之前的回答中。

于 2012-12-20T15:44:42.967 回答