C# 反序列化到XML中没有容器元素的列表中
在我所看到的所有使用C# 反序列化到XML中没有容器元素的列表中,c#,.net,xml-serialization,C#,.net,Xml Serialization,在我所看到的所有使用XmlSerializer的示例中,每当出现列表或数组时,都会出现如下某种容器元素: [XmlRoot("GeocodeResponse")] public class GeocodeResponse { public GeocodeResponse() { this.Results = new List<Result>(); } [XmlElement("status")]
XmlSerializer
的示例中,每当出现列表或数组时,都会出现如下某种容器元素:
[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没有类似于上述内容的容器。它只是开始重复元素。(顺便说一句,XML实际上来自谷歌的地理编码API)
因此,我有如下的XML:
[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; }
}
好啊
地点
政治的
格拉斯哥,英国格拉斯哥市
格拉斯哥
格拉斯哥
地点
政治的
东丹巴顿郡
东丹巴顿郡
行政区第三级
政治的
正如您在结果中看到的,type元素在没有XmlSerializer所期望的任何类型元素(或者至少是我看到的所有文档和示例)的情况下重复。地址_组件也是如此
我目前拥有的代码如下所示:
[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; }
}
[XmlRoot(“GeocodeResponse”)]
公共类地理编码响应
{
公共地理代码响应()
{
this.Results=新列表();
}
[XmlElement(“状态”)]
公共字符串状态{get;set;}
[XmlArray(“结果”)]
[XmlArrayItem(“结果”,typeof(结果))]
公共列表结果{get;set;}
}
每次尝试对XML进行反序列化时,结果列表中都没有项目
你能建议我如何让它工作,因为我目前没有看到它吗?使用
[XmlElement("result")]
public List<Result> Results { get; set; }
[XmlElement(“结果”)]
公共列表结果{get;set;}
它起作用了——我不敢相信我竟然错过了它这么简单。啊!我尝试过使用它,出于某种原因,它只是抓住了项目列表中的第一个。我不知道为什么,这真的让人恼火。仅供参考:这个确切的问题是昨天发布的。在xml序列化标记下,在这一个和这一个之间有一个中间Q。而且,这也不是第一次有人问/回答它了。哦,我找不到了!我先搜索,然后在输入问题标题时点击所有建议。