C# 反序列化XMLDocument对象的特定节点

C# 反序列化XMLDocument对象的特定节点,c#,C#,我有下面的一段代码,它接受XMLDocument对象和代码下面提到的格式的xml。我想读取bouding框中4个标签的值: 南纬等 public static void readXML(XmlDocument document) { if (document == null) { throw new ArgumentNullException("document"); } XmlNo

我有下面的一段代码,它接受XMLDocument对象和代码下面提到的格式的xml。我想读取bouding框中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>

- 
版权所有©2012 Microsoft及其供应商。版权所有。未经微软公司明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。
http://dev.virtualearth.net/Branding/logo_powered_by.png 
200
好啊
有效存款
f651e16fe1204e12b848084d73f5148d | SINM001008 | 02.00.83.1900 | SINMSNVM001115,SINMSNVM001124
- 
- 
1.
- 
- 

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

我们应该做到这一点。当然,亨克·霍尔特曼在早些时候的回答中赢得了很多赞誉

您能给出这种特殊情况下的使用示例吗?您的XML无效,因为您不能使用数字作为元素。如果您认为当前的答案没有帮助,那么发布更详细的XML示例也可能会有所帮助。以及您正试图从中提取的确切值。我已经发布了实际的XML文档。标题中的“反序列化”一词可能会误导您-您似乎只需要特定节点的值:)
        XmlNode specificNode = document.SelectSingleNode("/Response/ResourceSets/ResourceSet/Resources/Location/BoundingBox");
        if (specificNode != null)
        {
            foreach (XmlNode child in specificNode.ChildNodes)
            {
                Console.WriteLine(child.InnerText);
            }
        }