如何反序列化包含列表的xml文件?

时间:2017-11-30 06:32:02

标签: c# c#-4.0 c#-3.0

我正在尝试反序列化包含id列表的xml,但是它给了我错误"无法生成临时类(结果= 1)",请帮帮我。 以下是我的xml文件格式:

<?xml version="1.0" encoding="UTF-8"?>
<identifiers> 
<Module Name="Doors_Module1" Path="Doors_Module1 ">
    <id value="16"/>
    <id value="15"/>
    <id value="14"/>
    <id value="13"/>
    <id value="12"/>
    <id value="11"/>
    <id value="10"/>
    <id value="9"/>
    <id value="17"/>
    <id value="8"/>
    <id value="7"/>
    <id value="6"/>
    <id value="5"/>
    <id value="4"/>
    <id value="3"/>
    <id value="2"/>
    <id value="1"/>
</Module>
</identifiers>

以下是我的反序列化xml:

的类
 public class HelperAllIdentifiers
{
    [Serializable, XmlRoot("identifiers")]
    public class identifiers
    {
        public Module Module { get; set; }

    }
    [XmlRoot("Module")]
    public class Module
    {
        [XmlAttribute("Name")]
        public string Name
        {
            get;
            set;
        }

        [XmlArrayItem("id", Type = typeof(Attribute))]
        public List<IdValue> FieldList;// { get; set; }
        public Attribute[] ids { get; set; }
    }

    [XmlRoot("id")]
    public class IdValue
    {
        [XmlAttribute("value")]// Type=typeof(Attribute))]
        public string Value { get; set; }
    }
}

感谢高级。

1 个答案:

答案 0 :(得分:0)

以下代码为我工作:

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

namespace ConsoleWinForm
{
    [XmlRoot(ElementName = "id")]
    public class Id
    {
        [XmlAttribute(AttributeName = "value")]
        public string Value { get; set; }
    }

    [XmlRoot(ElementName = "Module")]
    public class Module
    {
        [XmlElement(ElementName = "id")]
        public List<Id> Id { get; set; }
        [XmlAttribute(AttributeName = "Name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName = "Path")]
        public string Path { get; set; }
    }

    [XmlRoot(ElementName = "identifiers")]
    public class Identifiers
    {
        [XmlElement(ElementName = "Module")]
        public Module Module { get; set; }
    }

    public class Program
    {
        public static void Main()
        {
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                            <identifiers> 
                            <Module Name=""Doors_Module1"" Path=""Doors_Module1 "">
                                <id value=""16""/>
                                <id value=""15""/>
                                <id value=""14""/>
                                <id value=""13""/>
                                <id value=""12""/>
                                <id value=""11""/>
                                <id value=""10""/>
                                <id value=""9""/>
                                <id value=""17""/>
                                <id value=""8""/>
                                <id value=""7""/>
                                <id value=""6""/>
                                <id value=""5""/>
                                <id value=""4""/>
                                <id value=""3""/>
                                <id value=""2""/>
                                <id value=""1""/>
                            </Module>
                            </identifiers>";

            using (var rdr = new StringReader(xml))
            {
                var srlzr = new XmlSerializer(typeof(Identifiers));
                var result = srlzr.Deserialize(rdr) as Identifiers;
            }
        }
    }
}