C# 创建可序列化的类-复杂对象
我需要将数据序列化为XML,但我在解决如何实现这一点上遇到了真正的困难。(在Visual Studio中) 我需要创建如下结构所示的这种类型的XML。但是对象FormType包含ILists,它不会序列化C# 创建可序列化的类-复杂对象,c#,xml,serialization,C#,Xml,Serialization,我需要将数据序列化为XML,但我在解决如何实现这一点上遇到了真正的困难。(在Visual Studio中) 我需要创建如下结构所示的这种类型的XML。但是对象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/XMLSche
<?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>
我无法将IList更改为列表-那么还有其他方法可以做到这一点吗
这是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; }
}
}
[可序列化]
公共类FormType:实体
{
公共虚拟产品代码产品{get;set;}
公共虚拟字符串DisplayName{get;set;}
公共虚拟字符串FormNumber{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟布尔活动{get;set;}
私有IList_versions=新列表();
公共虚拟IList版本
{
获取{return\u versions;}
设置{u versions=value;}
}
}
要实现这一点,请使用可序列化类型而不是IEnumerable
,或者使用列表
[编辑]当然,FormType也必须实现ISerializable。因此,对于您提供的资源,我附带了一个示例 Foo
[Serializable]
[XmlRoot("Foo")]
public class Foo
{
[XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName = "Bar")]
public List<Bar> BarList { 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>
Foo f=new Foo();
f、 BarList=新列表();
f、 添加(newbar(){Property1=“s”,Property2=“2”});
f、 添加(newbar(){Property1=“s”,Property2=“2”});
FileStream fs=newfilestream(“c:\\test.xml”,FileMode.OpenOrCreate);
System.Xml.Serialization.XmlSerializer s=new System.Xml.Serialization.XmlSerializer(typeof(Foo));
s、 序列化(fs,f);
输出
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>
s
2.
s
2.
这显示了如何使用自定义类列表序列化xml
你也可以参考:
编辑:
你还可以:
[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; } }
}
[可序列化]
[XmlRoot]
公共类FormXml
{
公共字符串ImportID{get;set;}
公共字符串环境{get;set;}
公共字符串DateExported{get;set;}
[XmlIgnore]
公共IEnumerable FormType{get;set;}
[XmlElement,可浏览(false),可编辑可浏览(EditorBrowsableState.Never)]
公共列表Foo{get{return FormType.ToList()}set{FormType=value;}
}
“1…..因为它是一个接口”您能给我看一下FormType吗?您是否也用[Serializable]标记了FormType?您使用的是哪个IDE?Visual Studio 2013-我添加了FormType.cs,其中还包含FormTypeVersion对象的ILists。您不能将其更改为列表的原因是什么?请查看编辑->特殊粘贴->将XML粘贴为类生成的类。可能会引导你去做你需要做的事情,但它不起作用,可能是因为FormType本身包含具有IList的其他对象?FormType应该实现ISerializable,但这使用的是列表而不是IList?在调用serialize之前使用ToList获取项目列表如何?XmlSerializer不支持系统中定义的大多数类型。据我所知,集合命名空间IEnumerable就是其中之一这不起作用,但仍然有一个关于FormTypeVersion的错误。FormType包含一个FormTypeVersions的IList(参见OP),我认为您必须使内部的每个类都可以序列化,因此如果您序列化formxl,那么您必须执行FormType和formtypeversion
[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; } }
}