使用 C# 反序列化数组元素内的数组元素

发布于 2024-10-17 04:50:22 字数 2534 浏览 9 评论 0原文

我的 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 中的 XML:

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

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

谢谢!

My XML file looks something like this:

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

I'm trying to get to the items within the ItemCollection, but so far, not luck at all; this is what my code looks like:

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

And my objects look like:

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

I can get the version just fine, and the hierarchy looks fine, but Name and Age from the Item object are always null. I've tried XmlElement instead of the XmlArray, but that doesn't work either.

Any help on how to achieve this will be much appreciated!!!

EDIT: The example I gave was a simplification of the XML that I receive: it is actually a call to the location REST service from the BING API; the XML I get looks like the one in this URL:

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

and what I'm trying to put in my structures are the information within the Location
element.

My real objects look like this:

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

Hopefully this will clarify even further what I'm trying to achieve here.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀子花开つ 2024-10-24 04:50:22

也许您的输入文件不正确,因为我相信您的代码可以工作。

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

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

    }
  }
}

Maybe your input file is incorrect because I believe your code works.

Here's a quick console app I wrote to test. Name and Age are both deserialized correctly.

Note: I assume your actual xml is not missing the closing tag, which is missing from your example.

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

    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文