使用c#反序列化数组元素中的数组元素

时间:2011-02-11 02:42:39

标签: c# xml xml-deserialization bing-api

我的XML文件看起来像这样:

<MyXml>
<Version> 9.3.2 </Version>
<Resources>      
  <Sets>
    <ItemCollection>
       <Item>
           <Name> Name </Name>
           <Age> 66 </Age>
       </Item>
     </ItemCollection>
   </Sets>
</Resources>

我正在尝试查看ItemCollection中的项目,但到目前为止,根本没有运气;这就是我的代码:

Stream reader = new FileStream(fileLocation, FileMode.Open);
XmlSerializer s = new XmlSerializer(typeof(MyClass));
var items = s.Deserialize(reader) as MyClass;

我的对象看起来像:

[Serializable]
[XmlRoot("MyXml")]
public class MyClass
{
   [XmlElement("Version")]
   public string Version { get; set; }

   [XmlElement("Resources")]
   public List<Resources> Resources{ get; set; }
}

[Serializable]
public class Resources
{       
   [XmlElement("Sets")]
   public List<Sets> Sets { get; set; }
}

[Serializable]
public class Sets
{
    [XmlArray(ElementName = "ItemCollection")]
    [XmlArrayItem("Item")]
    public List<Item> Items { get; set; }
}

[Serializable]
public class Item
{
    [XmlElement("Name")]
    public string Name{ get; set; }

    [XmlElement("Age")]
    public string Age { get; set; }
}

我可以很好地获得版本,并且层次结构看起来很好,但Item对象中的Name和Age始终为null。我尝试过XmlElement而不是XmlArray,但这也不起作用。

如何实现这一目标的任何帮助将不胜感激!

编辑:我给出的示例是我收到的XML的简化:它实际上是从BING API调用位置REST服务;我得到的XML看起来像这个URL中的那个:

http://msdn.microsoft.com/en-us/library/ff701710.aspx

我想要在我的结构中放置的是位置内的信息 元件。

我的真实物体看起来像这样:

[Serializable]
[XmlRoot("Response")]
public class LocationService
{
   [XmlElement("StatusCode")]
   public string Code{ get; set; }

   [XmlElement("ResourceSets")]
   public List<ResourceSets> ResourceSets{ get; set; }
}

[Serializable]
public class ResourceSets
{       
   [XmlElement("ResourceSet")]
   public List<ResourceSet> ResourceSet { get; set; }
}

[Serializable]
public class ResourceSet
{
    [XmlArray(ElementName = "Resources")]
    [XmlArrayItem("Location")]
    public List<Location> Locations { get; set; }
}

[Serializable]
public class Location
{
    [XmlElement("Latitude")]
    public string Latitude{ get; set; }

    [XmlElement("Longitude")]
    public string Longitude{ get; set; }
}

希望这将进一步澄清我在这里想要实现的目标。

谢谢!

1 个答案:

答案 0 :(得分:1)

也许您的输入文件不正确,因为我相信您的代码有效。

这是我编写的一个快速控制台应用程序。名称和年龄都正确反序列化。

注意:我假设你的实际xml没有遗漏你的例子中缺少的结束标记。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
  [Serializable]
  [XmlRoot("MyXml")]
  public class MyClass
  {
    [XmlElement("Version")]
    public string Version { get; set; }

    [XmlElement("Resources")]
    public List<Resources> Resources { get; set; }
  }

  [Serializable]
  public class Resources
  {
    [XmlElement("Sets")]
    public List<Sets> Sets { get; set; }
  }

  [Serializable]
  public class Sets
  {
    [XmlArray(ElementName = "ItemCollection")]
    [XmlArrayItem("Item")]
    public List<Item> Items { get; set; }
  }

  [Serializable]
  public class Item
  {
    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("Age")]
    public string Age { get; set; }
  }

  class Program
  {
    static void Main(string[] args)
    {
      string xml = 
      @"<MyXml>
         <Version> 9.3.2 </Version>
         <Resources>      
           <Sets>
             <ItemCollection>
               <Item>
                 <Name> Name </Name>
                 <Age> 66 </Age>
               </Item>
             </ItemCollection>
           </Sets>
         </Resources>
       </MyXml>";

      MemoryStream str = new MemoryStream( UTF8Encoding.UTF8.GetBytes( xml ) );

      XmlSerializer s = new XmlSerializer(typeof(MyClass));
      var items = s.Deserialize( str ) as MyClass;

      Console.Write( "Done" );

    }
  }
}