无法将XML反序列化为List<>

时间:2014-08-22 22:09:22

标签: c# xml list xml-serialization

我在尝试使用以下XML代码时遇到问题。首先,例外是​​<items xmlns=''> was not expected.,我似乎已经能够通过指定XmlRootAttribute来解决这个问题。现在,它回来了一个空的List<Items>,我无法弄清楚为什么。有什么想法吗?

XML示例

<?xml version="1.0"?>
<items>
    <item>
        <version>1.0</version>
        <title>Example</title>
        <changelog>http://example.com/changelog.txt</changelog>
        <url>http://www.example.com/download/</url>
    </item>
</items>

XML反序列化代码

Stream appCastStream = webResponse.GetResponseStream();
UpdateXML updateXml = new UpdateXML();

var rootAttribute = new XmlRootAttribute();
rootAttribute.ElementName = "items";
rootAttribute.IsNullable = true;

XmlSerializer serializer = new XmlSerializer(typeof(UpdateXML), rootAttribute);

try
{
    using (XmlTextReader reader = new XmlTextReader(appCastStream))
    {
        if (serializer.CanDeserialize(reader))
        {
            updateXml = (UpdateXML)serializer.Deserialize(reader);
        }
        else
        {
            throw new Exception("Update file is in the wrong format.");
        }
    }
}
catch (Exception ex)
{
    Debug.WriteLine("The following error occurred trying to check for updates: {0}", new object[] { ex.Message });

    return;
}

UpdateXML代码

public class UpdateXML
{
    public class Item
    {
        private string _versionString;
        private string _title;
        private string _changelog;
        private string _url;

        [XmlElement("version")]
        public string VersionString
        {
            get { return this._versionString; }
            set { this._versionString = value; }
        }

        public Version Version
        {
            get 
            {
                if (string.IsNullOrEmpty(this._versionString))
                    return null;

                return new Version(this._versionString); 
            }
        }

        [XmlElement("title")]
        public string Title
        {
            get { return this._title; }
            set { this._title = value; }
        }

        [XmlElement("changelog")]
        public string ChangeLog
        {
            get { return this._changelog; }
            set { this._changelog = value; }
        }

        [XmlElement("url")]
        public string URL
        {
            get { return this._url; }
            set { this._url = value; }
        }
    }

    private List<Item> _items = new List<Item>();

    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> Items
    {
        get { return this._items; }
        set { this._items = value; }
    }

    public UpdateXML()
    {

    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题在于你的XML确实没有&#34; root&#34; element--顶级元素是一个数组。 This answer应用于您的问题似乎解决了问题(同时删除了传递给rootAttribute的{​​{1}}参数):

XmlSerializer

示例: https://dotnetfiddle.net/MAet5y

如果您能够修改XML,则可以添加根元素,这样也可以解决问题。

例如,如果您将当前XML包装在[XmlRootAttribute("items")] public class UpdateXML { private List<Item> _items; [XmlElement("item")] public List<Item> Items { get { return this._items; } set { this._items = value; } } } 中,然后按如下方式修改<root></root>类:

UpdateXML

示例: https://dotnetfiddle.net/6KfxA4