我需要将数据序列化为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更改为List - 那么还有另一种方法吗?
这是thr 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 :(得分:0)
实现此目的使用可序列化类型而不是IEnumerable<FormType>
,可能是List<FormType>
?
[edit]当然,FormType也必须实现ISerializable。
答案 1 :(得分: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
修改强> 你也可以:
[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; } }
}