如何反序列化数组

本文关键字:数组 反序列化 | 更新日期: 2023-09-27 18:33:53

这是我从一个网站得到的XML:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="[url]">
  <totalResults>1</totalResults>
  <movie code="134539" />
  <movie code="134540" />
</feed>

我的 C# 类:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[Serializable]
[XmlRoot("feed", Namespace = "[url]")]
public class FeedSearch
{
    [XmlElement("totalResults")]
    public int TotalResults
    { get; set; }
    [XmlArray("feed")]
    [XmlArrayItem("movie")]
    public List<MovieSearch> Movies
    { get; set; }
}

using System;
using System.Xml.Serialization;
[Serializable]
[XmlRoot("movie", Namespace = "[url]")]
public class MovieSearch
{
    [XmlAttribute("code")]
    public int Code
    { get; set; }
}

总结果总是很好地反序列化,但我的电影列表总是空的,为什么?

如何反序列化数组

而不是:

[XmlArray("feed")]
[XmlArrayItem("movie")]
public List<MovieSearch> Movies { get; set; }

尝试如下:

[XmlElement("movie")]
public List<MovieSearch> Movies { get; set; }