【问题标题】:Deserializing an array element within an array element using c#使用c#反序列化数组元素中的数组元素
【发布时间】:2011-02-11 02:42:39
【问题描述】:

我的 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

我试图在我的结构中放入的是 Location 中的信息 元素。

我的实物是这样的:

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

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

谢谢!

【问题讨论】:

  • 所以您可以生成该结构但不能读回它?
  • 您不需要 Serializable 属性,它旨在与 Formatter 的派生序列化程序(BinaryFormatter、SoapFormatter)一起使用。 XmlSerializer 仅在公共属性上中继。 IE。当且仅当它是公共的并且具有可访问的默认构造函数时,类才能通过 XmlSerializer 序列化。而且您也不需要元素名称与属性名称相同的 XmlElement 属性。顺便说一句,您是否忘记了 MyXml 的结束标记?因为没有它,XML 结构是无效的。
  • @Dmitry Lobanov 好吧,感谢您提供的所有信息。问问题最好的部分是你能学到的所有东西。该文件只是我按照真实 XML 文件的结构整理的一个示例。 @drachenstern:我从 REST 服务调用中获取 XML 文件。我认为让它变得简单会有助于解决问题,但我猜我离题了。我将编辑问题。
  • 我的错.. 代码工作正常,但我的 XML 有点不同.. 无论如何谢谢大家

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


【解决方案1】:

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

这是我为测试而编写的一个快速控制台应用程序。 Name 和 Age 都已正确反序列化。

注意:我假设您的实际 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" );

    }
  }
}

【讨论】:

  • @drachenstern:我编辑了问题以使其更清楚。谢谢你的评论。 @Chris Hogan:XML 应该没问题.. 我真的很惊讶代码可以工作.. 然后我不知道发生了什么!无论如何,我编辑了问题以提供更多信息,希望可以得到解决。
猜你喜欢
  • 1970-01-01
  • 2012-01-06
  • 1970-01-01
  • 2016-10-02
  • 2023-03-20
  • 2015-04-25
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
相关资源
最近更新 更多