我正在尝试将对象序列化为xml,我有以下错误:
: Could Not Serialize object to .\Sample.xml
内部例外是:
There was an error reflecting type 'SiteProvisioningFramework.Entities.SiteDefinition'.
序列化代码是:
static void Main(string[] args)
{
var siteDefinition = new SiteDefinition();
siteDefinition.Name = "ContosoIntranet";
siteDefinition.Version = "1.0.0.0";
siteDefinition.MasterPages = new List<SiteProvisioningFramework.MasterPage>()
{
new MasterPage(){
Name="seattle.master",
ServerFolder ="_catalogs/ContosoIntranet/",
UIVersion = "15",
Url="",
LocalFolder = ".MasterPages/seattle.master"
}
};
Utilities.XmlHelper.ObjectToXml(siteDefinition, @".\Sample.xml");
}
public static void ObjectToXml(object obj, string path_to_xml)
{
//serialize and persist it to it's file
try
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(obj.GetType());
FileStream fs = File.Open(
path_to_xml,
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.ReadWrite);
ser.Serialize(fs, obj);
}
catch (Exception ex)
{
throw new Exception(
"Could Not Serialize object to " + path_to_xml,
ex);
}
}
课程是:
public class SiteDefinition
{
[XmlAttribute ()]
public string Name { get; set; }
[XmlAttribute()]
public string Version { get; set; }
public List<MasterPage> MasterPages { get; set; }
public List<File> Files { get; set; }
public List<PageLayout> PageLayouts { get; set; }
public List<Feature> Features { get; set; }
public List<ContentType> ContentTypes { get; set; }
public List<StyleSheet> StyleSheets { get; set; }
}
public class MasterPage : File
{
[XmlAttribute()]
public string UIVersion { get; set; }
[XmlAttribute()]
public string MasterPageDescription { get; set; }
}
public class File
{
[XmlAttribute()]
public string Url { get; set; }
[XmlAttribute()]
public string Name { get; set; }
[XmlAttribute()]
public string LocalFolder { get; set; }
[XmlAttribute()]
public string ServerFolder { get; set; }
}
public class Field
{
public string Guid { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
}
public class Feature
{
public string Guid { get; set; }
}
public class ContentType
{
public string Guid { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
public List<Field> Fields { get; set; }
}
public class List
{
public List<ContentType> ContentTypes { get; set; }
public string Name { get; set; }
}
public class PageLayout : File
{
public string UIVersion { get; set; }
public string MasterPageDescription { get; set; }
}
public class StyleSheet : File
{
public string Name { get; set; }
}
public class Theme
{
public string Name { get; set; }
public string ColorFilePath { get; set; }
public string FontFilePath { get; set; }
public string BackgroundImagePath { get; set; }
public MasterPage MasterPage { get; set; }
}
任何想法?
答案 0 :(得分:4)
错误在于SiteDefinition
类中的一个属性 -
public List<ContentType> ContentTypes { get; set; }
System.Net.Mime.ContentType
显然无法序列化。如果您在其上放置XmlIgnore
属性,则代码运行正常。
[XmlIgnore]
public List<ContentType> ContentTypes { get; set; }
您的ContentType
是自定义类 - 所以不是这样。但是Name
类中的StyleSheet
属性隐藏了它继承自(File
)的类中的完全相同的属性 - 这导致序列化错误。