无法将对象序列化为.Sample.xml

本文关键字:Sample xml 序列化 对象 | 更新日期: 2023-09-27 18:01:30

我正在尝试将对象序列化为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; }
    }

任何想法?

无法将对象序列化为.Sample.xml

错误在于您的SiteDefinition类中的一个属性-

public List<ContentType> ContentTypes { get; set; }

A System.Net.Mime.ContentType显然不能序列化。如果您将XmlIgnore属性放在上面,则代码可以正常运行。

[XmlIgnore]
public List<ContentType> ContentTypes { get; set; }

编辑:

你的ContentType是一个自定义类-所以这不是它。但是你的StyleSheet类中的Name属性隐藏了它从(File)继承的类中的完全相同的属性-这导致序列化错误。