将XML反序列化为列表会产生空列表

时间:2016-03-25 13:10:56

标签: c# xml list

我一直在尝试将XML文件序列化为列表,但无论我做什么,列表最终都会计为0。

我一直在寻找Stack,就像我平常一样,尝试各种各样的事情,但我的智慧结束了。这里出了什么问题?

我的XML:


   ->   
   (unexpected transform)

Lijst_item对象:

<?xml version="1.0" encoding="utf-8"?>
<lijst>
  <lijst_item>
    <id>1</id>
    <naam>NAME REDACTED</naam>
    <archived>false</archived>
  </lijst_item>
  <lijst_item>
    <id>2</id>
    <naam>NAME REDACTED</naam>
    <archived>false</archived>
  </lijst_item>
  <lijst_item>
    <id>3</id>
    <naam>NAME REDACTED</naam>
    <archived>false</archived>
  </lijst_item>
</lijst>

用于序列化的代码:

[XmlType("Lijst_item")]
public class Lijst_item
{
    [XmlAttribute("id", DataType = "int")]
    public int ID { get; set; }

    [XmlElement("naam")]
    public string Name { get; set; }

    [XmlElement("archived", DataType ="boolean")]
    public bool isArchived { get; set; }

    public Lijst_item()
    {

    }

    public Lijst_item(int id, string name, bool archived)
    {
        this.ID = id;
        this.Name = name;
        this.isArchived = archived;
    }
}

Convert xml to List by Deserialize in c# 没有帮助我:究竟我做错了什么?我的XML格式错误吗?我的对象?我可以出于某种原因不使用List吗?

1 个答案:

答案 0 :(得分:0)

试试这个......

... Usings

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

Classes ...(使用您在http://xmltocsharp.azurewebsites.net/处的XML创建)

    [XmlRoot(ElementName = "lijst_item")]
    public class Lijst_item
    {
        [XmlElement(ElementName = "id")]
        public string Id { get; set; }
        [XmlElement(ElementName = "naam")]
        public string Naam { get; set; }
        [XmlElement(ElementName = "archived")]
        public string Archived { get; set; }
    }

    [XmlRoot(ElementName = "lijst")]
    public class Lijst
    {
        [XmlElement(ElementName = "lijst_item")]
        public List<Lijst_item> Lijst_item { get; set; }
    }

...代码

        string strXML = @"<?xml version=""1.0"" encoding=""utf-8""?>
                            <lijst>
                                <lijst_item>
                                    <id>1</id>
                                    <naam>NAME REDACTED</naam>
                                    <archived>false</archived>
                                </lijst_item>
                                <lijst_item>
                                    <id>2</id>
                                    <naam>NAME REDACTED</naam>
                                    <archived>false</archived>
                                </lijst_item>
                                <lijst_item>
                                    <id>3</id>
                                    <naam>NAME REDACTED</naam>
                                    <archived>false</archived>
                                </lijst_item>
                            </lijst>";

        byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
        MemoryStream ms1 = new MemoryStream(bufXML);

        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(Lijst));
        try
        {
            using (XmlReader reader = new XmlTextReader(ms1))
            {
                Lijst deserializedXML = (Lijst)serializer.Deserialize(reader);

            }// put a break point here and mouse-over Label1Text and Label2Text ….
        }
        catch (Exception ex)
        {
            throw;
        }