我需要将数据序列化为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; }
}
}
“1 .....因为它是一个接口”你能告诉我FormType吗?你有没有用[Serializable]标记FormType呢? – 2014-12-05 10:05:33
您正在使用哪种IDE? – 2014-12-05 10:06:23
Visual Studio 2013 - 我添加了FormType.cs,它也包含FormTypeVersion对象的ILists – user3437721 2014-12-05 10:10:07