XML 反序列化为列表

XML Deserialization into List

我正在尝试将 XML 反序列化为 C# 对象(包含对象数组)

这里是 类:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1015")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("I-collection", Namespace = "urn:xtk:queryDef", IsNullable = false)]
public partial class MyCollection
{

    private List<I> itemsField;

    public MyCollection()
    {
        this.itemsField = new List<I>();

    }

    [System.Xml.Serialization.XmlElementAttribute("I", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public List<I> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1015")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class I
{

    private string EMAILField;

    private string FIRST_NAMEField;

    private string INDIVIDUAL_KEYField;

    private string LANGUAGE_CODEField;

    private string LAST_NAMEField;

    private string USERNAMEField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string EMAIL
    {
        get
        {
            return this.EMAILField;
        }
        set
        {
            this.EMAILField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FIRST_NAME
    {
        get
        {
            return this.FIRST_NAMEField;
        }
        set
        {
            this.FIRST_NAMEField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string INDIVIDUAL_KEY
    {
        get
        {
            return this.INDIVIDUAL_KEYField;
        }
        set
        {
            this.INDIVIDUAL_KEYField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string LANGUAGE_CODE
    {
        get
        {
            return this.LANGUAGE_CODEField;
        }
        set
        {
            this.LANGUAGE_CODEField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string LAST_NAME
    {
        get
        {
            return this.LAST_NAMEField;
        }
        set
        {
            this.LAST_NAMEField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string USERNAME
    {
        get
        {
            return this.USERNAMEField;
        }
        set
        {
            this.USERNAMEField = value;
        }
    }
}

正在尝试反序列化

MyCollection result = Serialization.Deserialize<MyCollection>(xmle.OuterXml);

其中 xmle.OuterXml 是

<I-collection xmlns="urn:xtk:queryDef">
  <I EMAIL="DFVCQD@DED.COM" FIRST_NAME="" INDIVIDUAL_KEY="5" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="UPed" />
  <I EMAIL="GZPP@TM.COM" FIRST_NAME="" INDIVIDUAL_KEY="6" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="GO" />
  <I EMAIL="IR0@MAIL.COM" FIRST_NAME="" INDIVIDUAL_KEY="7" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="ir0" />
  <I EMAIL="QSNFA@QSA.COM" FIRST_NAME="" INDIVIDUAL_KEY="8" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="UPlqfa" />
  <I EMAIL="JO7@HQ.COM" FIRST_NAME="" INDIVIDUAL_KEY="9" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="jon17" />
  <I EMAIL="AUTO-CTU_AA4849_JIE@COOLIT.ORG" FIRST_NAME="" INDIVIDUAL_KEY="10" LANGUAGE_CODE="N/D" LAST_NAME="" USERNAME="ctu_aa4849_jie" />
  <I EMAIL="YVMPYEUFREZZPMLF@TKBYFGDPMYSKLZBB.COM" FIRST_NAME="" INDIVIDUAL_KEY="11" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="rnopwakc" />
  <I EMAIL="ASDSD@SDAS.COM" FIRST_NAME="" INDIVIDUAL_KEY="12" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="TRIsd" />
  <I EMAIL="U2@X.COM" FIRST_NAME="" INDIVIDUAL_KEY="13" LANGUAGE_CODE="ENG" LAST_NAME="" USERNAME="ll20373196" />
  <I EMAIL="W@W.TEST" FIRST_NAME="Sa" INDIVIDUAL_KEY="14" LANGUAGE_CODE="FRA" LAST_NAME="w" USERNAME="wsp" />
</I-collection>

Serialization.Deserialize 这样做:

public static T Deserialize<T>(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentNullException("xml");
            }


            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringReader stream = new StringReader(xml))
            {
                try
                {
                    return (T)serializer.Deserialize(stream);
                }

                catch (Exception ex)
                { 
                    throw new InvalidOperationException("Failed to create object from xml string", ex);
                }
            }
        }

问题是它 returns 的对象是空的(我的意思是 itemsField 列表是空的)。这种反序列化实际上应该使用 XML 中的数据自动填充列表,对吧?还是我必须手动完成?

Form不正确;尝试:

[System.Xml.Serialization.XmlElementAttribute("I", Order = 0)]

试试这个

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"C:\temp\test.xml";
        static void Main(string[] args)
        {

            MyCollection myCollection = new MyCollection();

            XmlSerializer xs = new XmlSerializer(typeof(MyCollection.I_Collection));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            MyCollection.I_Collection newRoot = (MyCollection.I_Collection)xs.Deserialize(reader);

        }
    }
    public partial class MyCollection
    {
        [XmlRoot("I-collection")]
        public class I_Collection
        {
            [XmlElement("I")]
            public List<I> itemsField { get; set; }

        }

        [XmlRoot("I")]
        public partial class I
        {
            [XmlAttribute("EMAIL")]
            public string EMAILField { get; set; }

            [XmlAttribute("FIRST_NAME")]
            public string FIRST_NAMEField { get; set; }

            [XmlAttribute("INDIVIDUAL_KEY")]
            public string INDIVIDUAL_KEYField { get; set; }

            [XmlAttribute("LANGUAGE_CODE")]
            public string LANGUAGE_CODEField { get; set; }

            [XmlAttribute("LAST_NAME")]
            public string LAST_NAMEField { get; set; }

            [XmlAttribute("USERNAME")]
            public string USERNAMEField { get; set; }
        }
    }



}