【问题标题】:Creating a class which is serializable - complex objects创建一个可序列化的类 - 复杂对象
【发布时间】:2014-12-05 10:01:36
【问题描述】:

我需要将数据序列化为 XML,但我在弄清楚如何做到这一点时遇到了真正的麻烦。 (在 Visual Studio 中)

我需要创建这种类型的 XML,如下所示。但是 Object FormType 包含 ILists,它不会序列化。

<?xml version="1.0" encoding="utf-16"?>
<VersionXml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ImportId>1</ImportId>
<Environment>SIT</Environment>
<DateExported>12/2/2014</DateExported>
<FormType>
    <Id>4000</Id>
    <FormTypeVersion>
        <DisplayName>display name here</DisplayName>
        <FormNumber>12345<FormNumber>
        <Name>12345-abc<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>2<RevisionHistoryNumber>
    <FormTypeVersion>
    <FormTypeVersion>
        <DisplayName>display name here</DisplayName>
        <FormNumber>12345<FormNumber>
        <Name>12345-abc<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>3<RevisionHistoryNumber>
    <FormTypeVersion>
</FormType>
<FormType>
    <Id>4001</Id>
    <FormTypeVersion>
        <DisplayName>another one here</DisplayName>
        <FormNumber>456<FormNumber>
        <Name>456-bcd<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>3<RevisionHistoryNumber>
    <FormTypeVersion>
    <FormTypeVersion>
        <DisplayName>another one here</DisplayName>
        <FormNumber>456<FormNumber>
        <Name>456-bcd<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>1<RevisionHistoryNumber>
    <FormTypeVersion>
</FormType>
</VersionXml>

这是我尝试创建的类,但 FormType 不会序列化并出现反射器错误

[Serializable]
    public class FormXml
    {
        public string ImportID  { get; set; }
        public string Environment { get; set; }
        public string DateExported { get; set; }  
        public IEnumerable<FormType> FormType { get; set; }
    }

这是收到的错误:

Cannot serialize member FormXml.FormType of type System.Collections.Generic.IEnumerable`1..... because it is an interface.

我无法将 IList 更改为 List - 那么还有其他方法吗?

这里是 FormType.cs

[Serializable]
    public class FormType : Entity
    {
        public virtual ProductCode Product { get; set; }

        public virtual String DisplayName { get; set; }

        public virtual String FormNumber { get; set; }

        public virtual String Name { get; set; }

        public virtual Boolean Active { get; set; }

        private IList<FormTypeVersion> _versions = new List<FormTypeVersion>();

        public virtual IList<FormTypeVersion> Versions
        {
            get { return _versions; }
            set { _versions = value; }
        }
    }

【问题讨论】:

  • "1.....因为它是一个接口"你能告诉我FormType吗?你也用 [Serializable] 标记了 FormType 吗?
  • 您使用的是哪个 IDE?
  • Visual Studio 2013 - 我添加了 FormType.cs,其中还包含 FormTypeVersion 对象的 IList
  • 你不能将它们更改为 List 的原因是什么?
  • 查看从编辑 -> 选择性粘贴 -> 将 XML 粘贴为类中生成的类。可能会指导您完成您需要做的事情

标签: c# xml serialization


【解决方案1】:

要实现这一点,请使用可序列化类型而不是 IEnumerable&lt;FormType&gt;,也许是 List&lt;FormType&gt;

[edit] 当然,FormType 也必须实现 ISerializable。

【讨论】:

  • 试过这个还是不行,可能是因为FormType本身包含了其他带有IList的对象?
  • FormType 应该实现 ISerializable
【解决方案2】:

因此,对于我从您那里获得的资源,我提供了示例

Foo

[Serializable]
[XmlRoot("Foo")]
public class Foo
{
    [XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName = "Bar")]
    public List<Bar> BarList { get; set; }
}

条形

[Serializable]
public class Bar
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

要测试的代码

Foo f = new Foo();
f.BarList = new List<Bar>();
f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });
f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });

FileStream fs = new FileStream("c:\\test.xml", FileMode.OpenOrCreate);
System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(Foo));
s.Serialize(fs, f);

输出

<?xml version="1.0" ?> 
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <BarList>
        <Bar>
            <Property1>s</Property1> 
            <Property2>2</Property2> 
        </Bar>
        <Bar>
            <Property1>s</Property1> 
            <Property2>2</Property2> 
        </Bar>
    </BarList>
</Foo>

这是展示如何使用自定义类列表序列化 xml。

您也可以参考:

Serializing Lists of Classes to XML

XML Serialize generic list of serializable objects

XML Serialization and Deserialization

编辑: 你也可以:

[Serializable]
[XmlRoot]
public class FormXml
{
    public string ImportID  { get; set; }
    public string Environment { get; set; }
    public string DateExported { get; set; }  
    [XmlIgnore]
    public IEnumerable<FormType> FormType { get; set; }
    [XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public List<FormType> Foo { get { return FormType.ToList() } set { FormType = value; } }
}

【讨论】:

  • 但这使用列表而不是 IList?
  • 在调用serialize之前使用ToList获取项目列表怎么样?
  • XmlSerializer 不支持 System.Collections 命名空间中定义的大多数类型,因为我知道 IEnumerable 就是其中之一
  • 对不起,没有工作仍然收到有关 FormTypeVersion 的错误。 FormType 包含 FormTypeVersions 的 IList(参见 OP)
  • 我认为你必须让里面的每个类都可以序列化,所以如果你序列化 formxl 那么你必须使用 formtype 和 formtypeversion
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多