161

给定以下 XML:

<?xml version="1.0"?>
<user_list>
   <user>
      <id>1</id>
      <name>Joe</name>
   </user>
   <user>
      <id>2</id>
      <name>John</name>
   </user>
</user_list>

以及以下课程:

public class User {
   [XmlElement("id")]
   public Int32 Id { get; set; }

   [XmlElement("name")]
   public String Name { get; set; }
}

是否可以使用XmlSerializer将 xml 反序列化为List<User>? 如果是这样,我需要使用什么类型的附加属性,或者我需要使用什么附加参数来构造XmlSerializer实例?

User[]如果不太可取,数组 ( ) 是可以接受的。

4

7 回答 7

148

您可以简单地封装列表:

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

[XmlRoot("user_list")]
public class UserList
{
    public UserList() {Items = new List<User>();}
    [XmlElement("user")]
    public List<User> Items {get;set;}
}
public class User
{
    [XmlElement("id")]
    public Int32 Id { get; set; }

    [XmlElement("name")]
    public String Name { get; set; }
}

static class Program
{
    static void Main()
    {
        XmlSerializer ser= new XmlSerializer(typeof(UserList));
        UserList list = new UserList();
        list.Items.Add(new User { Id = 1, Name = "abc"});
        list.Items.Add(new User { Id = 2, Name = "def"});
        list.Items.Add(new User { Id = 3, Name = "ghi"});
        ser.Serialize(Console.Out, list);
    }
}
于 2009-03-03T21:05:23.167 回答
54

如果你用 装饰UserXmlType以匹配所需的大小写:

[XmlType("user")]
public class User
{
   ...
}

然后ctorXmlRootAttribute上的XmlSerializer可以提供所需的根并允许直接读入 List<>:

    // e.g. my test to create a file
    using (var writer = new FileStream("users.xml", FileMode.Create))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<User>),  
            new XmlRootAttribute("user_list"));
        List<User> list = new List<User>();
        list.Add(new User { Id = 1, Name = "Joe" });
        list.Add(new User { Id = 2, Name = "John" });
        list.Add(new User { Id = 3, Name = "June" });
        ser.Serialize(writer, list);
    }

...

    // read file
    List<User> users;
    using (var reader = new StreamReader("users.xml"))
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(List<User>),  
            new XmlRootAttribute("user_list"));
        users = (List<User>)deserializer.Deserialize(reader);
    }

学分:基于YK1回答

于 2014-02-14T11:23:51.200 回答
16

是的,它将序列化和反序列化 List<>。如果有疑问,请确保使用 [XmlArray] 属性。

[Serializable]
public class A
{
    [XmlArray]
    public List<string> strings;
}

这适用于 Serialize() 和 Deserialize()。

于 2009-03-03T21:05:00.300 回答
16

我想我找到了更好的方法。您不必将属性放入您的类中。我制作了两种以通用列表作为参数的序列化和反序列化方法。

看看(它对我有用):

private void SerializeParams<T>(XDocument doc, List<T> paramList)
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(paramList.GetType());

        System.Xml.XmlWriter writer = doc.CreateWriter();

        serializer.Serialize(writer, paramList);

        writer.Close();           
    }

private List<T> DeserializeParams<T>(XDocument doc)
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));

        System.Xml.XmlReader reader = doc.CreateReader();

        List<T> result = (List<T>)serializer.Deserialize(reader);
        reader.Close();

        return result;
    }

所以你可以序列化你想要的任何列表!您不需要每次都指定列表类型。

        List<AssemblyBO> list = new List<AssemblyBO>();
        list.Add(new AssemblyBO());
        list.Add(new AssemblyBO() { DisplayName = "Try", Identifier = "243242" });
        XDocument doc = new XDocument();
        SerializeParams<T>(doc, list);
        List<AssemblyBO> newList = DeserializeParams<AssemblyBO>(doc);
于 2011-02-01T07:54:21.607 回答
9

是的,它确实反序列化为 List<>。无需将其保存在数组中并将其包装/封装在列表中。

public class UserHolder
{
    private List<User> users = null;

    public UserHolder()
    {
    }

    [XmlElement("user")]
    public List<User> Users
    {
        get { return users; }
        set { users = value; }
    }
}

反序列化代码,

XmlSerializer xs = new XmlSerializer(typeof(UserHolder));
UserHolder uh = (UserHolder)xs.Deserialize(new StringReader(str));
于 2009-10-22T15:33:59.613 回答
5

不确定 List<T> 但数组肯定是可行的。一点点魔法让再次进入列表变得非常容易。

public class UserHolder {
   [XmlElement("list")]
   public User[] Users { get; set; }

   [XmlIgnore]
   public List<User> UserList { get { return new List<User>(Users); } }
}
于 2009-03-03T20:51:00.643 回答
2

怎么样

XmlSerializer xs = new XmlSerializer(typeof(user[]));
using (Stream ins = File.Open(@"c:\some.xml", FileMode.Open))
foreach (user o in (user[])xs.Deserialize(ins))
   userList.Add(o);    

不是特别花哨,但它应该可以工作。

于 2012-02-17T15:55:57.387 回答