反序列化到一个列表中没有一个XML容器元素 [英] Deserializing into a List without a container element in XML
问题描述
在我见过使用的所有例子的XmlSerializer
任何时候发生了,你有某种类似这样的容器元素的列表或数组:
In all the examples I've seen of using XmlSerializer
any time a list or array happens you have some sort of container element like this:
<MyXml>
<Things>
<Thing>One</Thing>
<Thing>Two</Thing>
<Thing>Three</Thing>
</Things>
</MyXml>
不过,XML我有没有类似于上面的的东西的容器。这只是开始重复元素。 (顺便说一句,XML实际上是从谷歌的地理编码API)
However, the XML I have has no container similar to Things above. It just starts repeating elements. (Incidentally, the XML is actually from Google's Geocode API)
所以,我有XML看起来像这样的:
So, I have XML that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>Glasgow, City of Glasgow, UK</formatted_address>
<address_component>
<long_name>Glasgow</long_name>
<short_name>Glasgow</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>East Dunbartonshire</long_name>
<short_name>East Dunbartonshire</short_name>
<type>administrative_area_level_3</type>
<type>political</type>
</address_component>
<!-- etc... -->
</result>
<result>
<!-- etc... -->
</result>
<result>
<!-- etc... -->
</result>
</GeocodeResponse>
正如你可以看到里面的结果的的的键入元素重复,没有任何的类型的是的XmlSerializer 的要素似乎期待(我见过或者至少所有的文档和示例)。这同样为_address_component _
As you can see inside result the type element repeats without any types element that XmlSerializer appears to expect (or at least all the documents and examples I've seen). The same goes for the _address_component_.
目前,我有代码看起来是这样的:
The code I currently have looks something like this:
[XmlRoot("GeocodeResponse")]
public class GeocodeResponse
{
public GeocodeResponse()
{
this.Results = new List<Result>();
}
[XmlElement("status")]
public string Status { get; set; }
[XmlArray("result")]
[XmlArrayItem("result", typeof(Result))]
public List<Result> Results { get; set; }
}
我每次尝试的反序列化的的XML我得到了我的结果为零的项目的_List _
Every time I attempt to deserialize the XML I get zero items in my Result _List_.
您可以建议我怎么可能得到这个工作,因为我目前没有看到呢?
Can you suggest how I may get this to work as I'm currently not seeing it?
推荐答案
使用
[XmlElement("result")]
public List<Result> Results { get; set; }
这篇关于反序列化到一个列表中没有一个XML容器元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!