2014-12-05 46 views
1

我需要將數據序列化爲XML,但是我很難解決如何做到這一點。 (在Visual Studio中)創建一個可序列化的類 - 複雜的對象

我需要在下面的結構中創建這種類型的XML。但Object FormType包含IList,它不會序列化。

<?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到一個列表 - 那麼還有另一種方法來做到這一點?

這裏是蘇氨酸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; } 
     } 
    } 
+0

「1 .....因爲它是一個接口」你能告訴我FormType嗎?你有沒有用[Serializable]標記FormType呢? – 2014-12-05 10:05:33

+0

您正在使用哪種IDE? – 2014-12-05 10:06:23

+0

Visual Studio 2013 - 我添加了FormType.cs,它也包含FormTypeVersion對象的ILists – user3437721 2014-12-05 10:10:07

回答

0

實現這一使用序列化的類型,而不是IEnumerable<FormType>,也許List<FormType>

[編輯]當然,FormType也必須實現ISerializable。

+0

試過這個,它沒有工作,也許是因爲FormType本身包含IList的其他對象? – user3437721 2014-12-05 10:09:17

+0

只是看到你編輯並聲明你不能使用列表 ... – 2014-12-05 10:09:17

+0

FormType應該實現ISerializable – 2014-12-05 10:10:10

0

所以對於我是從你有資源的問題,我用的例子來自

[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

編輯: 你可以ASLO:

[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; } } 
} 
+0

但是這使用列表而不是IList? – user3437721 2014-12-05 10:13:01

+0

如何在調用序列化之前使用ToList獲取項目列表? – 2014-12-05 10:16:27

+0

XmlSerializer不支持在System.Collections命名空間中定義的大多數類型,因爲我知道IEnumerable就是其中之一 – 2014-12-05 10:17:40