简体   繁体   English

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

[英]Error Deserializing XML Document 1,2

I'm having trouble de-serializing an XML document. 我在反序列化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>

I've copied and pasted into visual studio - using the Paste XML as class and that's created the following class 我已经将粘贴XML作为类复制并粘贴到了Visual Studio中,并创建了以下类

/// <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;
            }
        }
    }

I then run the following code to deserialise 然后,我运行以下代码进行反序列化

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

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

It's errors on the line r =... {" was not expected."} r = ... {上没有预期的错误。“}

I'm pretty certain the error lies in the class and the declarations at the top but I haven't been able to change it so it works, could anyone help? 我可以肯定的是错误位于类和顶部的声明中,但是我无法更改它,因此它可以工作,有人可以帮忙吗?

Change 更改

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

to

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

Its parameter is type of the object that this XmlSerializer can serialize. 它的参数是此XmlSerializer可以序列化的对象的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 反序列化xml字符串,xml文档中存在错误(1,2) - C# - Deserializing xml string, there is an error in xml document (1,2) 反序列化XML文件-xml文档中的错误(1,2) - Deserialize an XML file - an error in xml document (1,2) 将XML字符串反序列化为对象错误:xml文档中存在错误(1,2) - Deserialize XML string to Object Error : There is an Error in xml document (1,2) XML文档(1,1)中存在错误。 反序列化时 - There is an error in XML document (1, 1). while deserializing XML 文档 (0, 0) 中存在错误 - 将 XML 反序列化为 object 时出错 - There is an error in XML document (0, 0) - Error Deserializing XML to object 反序列化字符串XML和错误:XML文档中存在错误(1、2) - Deserializing a string XML and error:There is an error in XML document (1, 2) XML 文档 (1,2) 中存在错误,System.InvalidOperationException:<authorizationresult xlms:""> 没想到</authorizationresult> - There is an error in XML document (1,2) , System.InvalidOperationException: <AuthorizationResult xlms:""> was not expected 反序列化 XML 并在 XML 文档中出错 (2, 2) - Deserializing XML and Getting an Error in XML Document (2, 2) Xamarin:反序列化XML-“ XML文档中存在错误” - Xamarin: Deserializing XML - “There is an error in XML document” 反序列化时XML文档中存在错误(C#) - There is an error in XML document when deserializing(C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM