反序列化XML文档时出错1,2

时间:2015-08-12 07:44:13

标签: c# xml serialization

我无法反序列化XML文档。

<rules version="61">
    <emie>
        <domain exclude="false">facebook.com</domain>
        <domain exclude="false">google.com</domain>
        <domain exclude="false">bbc.co.uk</domain>
    </emie>
    <docMode>
        <domain docMode="7">outlook.com</domain>
        <domain docMode="7">yahoo.com</domain>
    </docMode>
</rules>

我已经复制并粘贴到visual studio中 - 使用Paste XML作为类,并创建了以下类

/// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class rules
    {

        private rulesDomain[] emieField;

        private rulesDomain1[] docModeField;

        private byte versionField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("domain", IsNullable = false)]
        public rulesDomain[] emie
        {
            get
            {
                return this.emieField;
            }
            set
            {
                this.emieField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("domain", IsNullable = false)]
        public rulesDomain1[] docMode
        {
            get
            {
                return this.docModeField;
            }
            set
            {
                this.docModeField = value;
            }
        }

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

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rulesDomain
    {

        private bool excludeField;

        private string valueField;

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

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

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rulesDomain1
    {

        private string docModeField;

        private string valueField;

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

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

然后我运行以下代码来反序列化

XmlSerializer mySerializer = new XmlSerializer(typeof(XmlSerializer));
            Classes.rules r;

            using (StreamReader reader = new StreamReader(@"file.xml"))
            {
                r = (Classes.rules)mySerializer.Deserialize(reader);
            }

线路上的错误r = ...... {&#34;没想到。&#34;}

我很确定错误在于班级和顶部的声明,但我还没有能够改变它以便它起作用,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

更改

XmlSerializer mySerializer = new XmlSerializer(typeof(XmlSerializer));

XmlSerializer mySerializer = new XmlSerializer(typeof(Classes.rules));

它的参数是此XmlSerializer可以序列化的对象的类型。