【问题标题】:How to deserialize XML file which contains list?如何反序列化包含列表的 XML 文件?
【发布时间】:2017-11-30 06:32:01
【问题描述】:

我正在尝试反序列化包含 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; }
    }
}

【问题讨论】:

  • 我不确定这是否是根本原因,但您的 Module 类与 XML 文件的内容不同:没有响应属性“Path”的属性。
  • 实际上我的结果中不需要属性“Path”。这就是为什么我不在课堂上使用该属性

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


【解决方案1】:

以下代码对我有用:

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;
            }
        }
    }
}

【讨论】:

  • 当我必须将 Xml 序列化为 C# 类时,我会这样做。 xmltocsharp.azurewebsites.net
  • 感谢您的快速回复 prateek,但现在我收到错误:未处理的异常:System.InvalidOperationException:XML 文档中存在错误(2、2)。 ---> System.InvalidOperationException: 不是预期的。
猜你喜欢
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
相关资源
最近更新 更多