浅谈序列化之 XmlSerializer

来源:互联网 发布:淘宝店铺标题优化 编辑:程序博客网 时间:2024/05/17 02:50

在程序设计中,有时需要保存当前对象的状态信息,待需要时直接转换为对象。此处记录 XmlSerializer 类的相关操作,即序列化为xml 和反序列化为 object.

1、序列化对象并保存到 valid.xml 文件

            ElementConfig config = new ElementConfig();
            Comment cmt = new Comment();
            cmt.CreatedBy = "Zhao";
            cmt.CreatedDate = DateTime.Now;
            cmt.Guid = new Guid();
            cmt.Message = "Hello";
            config.Comments = new Comment[] { cmt};
            BpmPropertyText bpmText = new BpmPropertyText();
            bpmText.UniqueName = "WWW";
            bpmText.Visible = true;
            config.Properties = new BpmProperty[] { bpmText};
            XmlSerializer ser = new XmlSerializer(typeof(ElementConfig));
            FileStream fs = new FileStream("valid.xml", FileMode.Create, FileAccess.Write);
            ser.Serialize(fs,config);

2、读取 valid.xml 并反序列化

            XmlSerializer ser = new XmlSerializer(typeof(ElementConfig));
            FileStream fs = File.Open("valid.xml", FileMode.Open, FileAccess.Read);
            ElementConfig o = (ElementConfig)ser.Deserialize(fs);
            fs.Close();

相关类的定义:

   #region ElementConfig
    /// <summary>
    /// The ElementConfig
    /// </summary>
    [XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public class ElementConfig
    {
        /// <summary>
        /// The Comments.
        /// </summary>
        public Comment[] Comments;

        /// <summary>
        /// The Folder.
        /// </summary>
        [XmlAttribute]
        public string Folder;

        /// <summary>
        /// The ElementVersion.
        /// </summary>
        public ElementVersion Version;

        /// <summary>
        /// The BpmProperties' Array
        /// </summary>
        public BpmProperty[] Properties;

    }
    #endregion

    #region Comment
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(AnnotationComment))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class Comment
    {

        private System.Guid guidField;

        private string createdByField;

        private System.DateTime createdDateField;

        private string messageField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public System.Guid Guid
        {
            get
            {
                return this.guidField;
            }
            set
            {
                this.guidField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string CreatedBy
        {
            get
            {
                return this.createdByField;
            }
            set
            {
                this.createdByField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public System.DateTime CreatedDate
        {
            get
            {
                return this.createdDateField;
            }
            set
            {
                this.createdDateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Message
        {
            get
            {
                return this.messageField;
            }
            set
            {
                this.messageField = value;
            }
        }
    }
    #endregion

    #region ElementVersion
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class ElementVersion
    {

        private int majorField;

        private int minorField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public int Major
        {
            get
            {
                return this.majorField;
            }
            set
            {
                this.majorField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public int Minor
        {
            get
            {
                return this.minorField;
            }
            set
            {
                this.minorField = value;
            }
        }
    }

    #endregion

    #region BpmProperty
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyBool))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyLinkBase))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyHyperlink))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyHyperlink2))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyUser))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyText))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyLongText))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyDate))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyList))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyDecimal))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public abstract partial class BpmProperty
    {

        private string uniqueNameField;

        private string displayNameField;

        private string descriptionField;

        private bool requiredField;

        private bool visibleField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string UniqueName
        {
            get
            {
                return this.uniqueNameField;
            }
            set
            {
                this.uniqueNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string DisplayName
        {
            get
            {
                return this.displayNameField;
            }
            set
            {
                this.displayNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Description
        {
            get
            {
                return this.descriptionField;
            }
            set
            {
                this.descriptionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool Required
        {
            get
            {
                return this.requiredField;
            }
            set
            {
                this.requiredField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool Visible
        {
            get
            {
                return this.visibleField;
            }
            set
            {
                this.visibleField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyBool
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyBool : BpmProperty
    {

        private bool valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyLinkBase
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyHyperlink))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(BpmPropertyHyperlink2))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyLinkBase : BpmProperty
    {

        private string hyperlinkField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Hyperlink
        {
            get
            {
                return this.hyperlinkField;
            }
            set
            {
                this.hyperlinkField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyHyperlink
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyHyperlink : BpmPropertyLinkBase
    {
    }
    #endregion

    #region BpmPropertyHyperlink2
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyHyperlink2 : BpmPropertyLinkBase
    {
    }
    #endregion

    #region BpmPropertyUser
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyUser : BpmProperty
    {

        private string loginField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Login
        {
            get
            {
                return this.loginField;
            }
            set
            {
                this.loginField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyText
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyText : BpmProperty
    {

        private string textField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyLongText
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyLongText : BpmProperty
    {

        private string textField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }
    }


    #endregion

    #region  BpmPropertyDate
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyDate : BpmProperty
    {

        private System.DateTime dateField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public System.DateTime Date
        {
            get
            {
                return this.dateField;
            }
            set
            {
                this.dateField = value;
            }
        }
    }
    #endregion

    #region BpmPropertyList
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyList : BpmProperty
    {

        private BpmPropertyListItem[] listItemsField;

        private int selectedIndexField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
        public BpmPropertyListItem[] ListItems
        {
            get
            {
                return this.listItemsField;
            }
            set
            {
                this.listItemsField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public int SelectedIndex
        {
            get
            {
                return this.selectedIndexField;
            }
            set
            {
                this.selectedIndexField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyDecimal
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyDecimal : BpmProperty
    {

        private decimal decimalField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal Decimal
        {
            get
            {
                return this.decimalField;
            }
            set
            {
                this.decimalField = value;
            }
        }
    }

    #endregion

    #region BpmPropertyListItem
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class BpmPropertyListItem
    {

        private string nameField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    #endregion

    #region  AnnotationComment
    [System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.648")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.microsoft.com/performancepoint/scorecards")]
    public partial class AnnotationComment : Comment
    {

        private string titleField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }
    }


    #endregion