使用 C# 反序列化数组元素内的数组元素
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您的输入文件不正确,因为我相信您的代码可以工作。
这是我编写来测试的一个快速控制台应用程序。名称和年龄均已正确反序列化。
注意:我假设您的实际 xml 没有缺少结束标记,而您的示例中缺少该结束标记。
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.