简体   繁体   English

C# 反序列化 XML 为 Model Class 错误 -<xmlns=“”> 没有预期</xmlns=“”>

[英]C# Deserialize XML to Model Class error - <xmlns=“”> Not expected

I'm trying to take an XML string and deserialize it and bind it to my Model class.我正在尝试获取 XML 字符串并将其反序列化并将其绑定到我的 Model class。 But I am getting this error:但我收到此错误:

System.InvalidOperationException: <requisitions xmlns=''> was not expected System.InvalidOperationException: <requisitions xmlns=''>不是预期的

This is the C# where I'm calling deserialize:这是我调用反序列化的 C#:

var deserializer = new XmlSerializer(typeof(JobsModel),new XmlRootAttribute("requisition"));
var jobs = new JobsModel();

using (var reader = new StringReader(xmlStr))
{
    jobs = (JobsModel)deserializer.Deserialize(reader);
}

My XML is formatted like this:我的 XML 的格式如下:

<requisitions>
    <requisition>
        <start_date></start_date>
        <end_date></end_date>
        <title></title>
        <precise_location>
            <address1/>
            <city></city>
            <display_city/>
            <country></country>
        </precise_location>
        <contract_type></contract_type>
        <experience></experience>
        <job_type></job_type>
    </requisition>
    <requisition>
        <start_date></start_date>
        <end_date></end_date>
        <title></title>
        <precise_location>
            <address1/>
            <city></city>
            <display_city/>
            <country></country>
        </precise_location>
        <contract_type></contract_type>
        <experience></experience>
        <job_type></job_type>
    </requisition>
</requisitions>

And the class I'm trying to deserialize to is this:我试图反序列化的 class 是这样的:

public class JobsModel
{
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
    public partial class requisition
    {
        [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
        public System.DateTime start_date { get; set; }

        [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
        public System.DateTime end_date { get; set; }

        public string title { get; set; }
        public requisitionPrecise_location precise_location { get; set; }
        public string contract_type { get; set; }
        public object experience { get; set; }
        public object job_type { get; set; }
    }

    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class requisitionPrecise_location
    {
        public object address1 { get; set; }
        public string city { get; set; }
        public object display_city { get; set; }
        public string country { get; set; }
    }
}

Solution解决方案

I used Visual Studio's Paste Special feature to generate the models.我使用 Visual Studio 的Paste Special功能来生成模型。 But where I went wrong was the XML I had copied to the clipboard only had one Requisition element.但是我出错的地方是我复制到剪贴板的 XML 只有一个Requisition元素。 So Visual Studio didn't generate the class the way I needed if I'm going to have multiple Requisitions.因此,如果我将有多个申请,Visual Studio 没有按照我需要的方式生成 class。

You shouldn't really modify the generated classes.您不应该真正修改生成的类。 You can rename the root object if you want.如果需要,您可以重命名根 object。 But make small changes testing each one.但是做一些小的改变来测试每一个。 Paste-special + deserialize works as expected with that XML to start with.特殊粘贴 + 反序列化与 XML 一起按预期工作。 EG例如

using System;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApp24
{

    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class requisitions
    {

        private requisitionsRequisition[] requisitionField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("requisition")]
        public requisitionsRequisition[] requisition
        {
            get
            {
                return this.requisitionField;
            }
            set
            {
                this.requisitionField = value;
            }
        }
    }

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

        private object start_dateField;

        private object end_dateField;

        private object titleField;

        private requisitionsRequisitionPrecise_location precise_locationField;

        private object contract_typeField;

        private object experienceField;

        private object job_typeField;

        /// <remarks/>
        public object start_date
        {
            get
            {
                return this.start_dateField;
            }
            set
            {
                this.start_dateField = value;
            }
        }

        /// <remarks/>
        public object end_date
        {
            get
            {
                return this.end_dateField;
            }
            set
            {
                this.end_dateField = value;
            }
        }

        /// <remarks/>
        public object title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }

        /// <remarks/>
        public requisitionsRequisitionPrecise_location precise_location
        {
            get
            {
                return this.precise_locationField;
            }
            set
            {
                this.precise_locationField = value;
            }
        }

        /// <remarks/>
        public object contract_type
        {
            get
            {
                return this.contract_typeField;
            }
            set
            {
                this.contract_typeField = value;
            }
        }

        /// <remarks/>
        public object experience
        {
            get
            {
                return this.experienceField;
            }
            set
            {
                this.experienceField = value;
            }
        }

        /// <remarks/>
        public object job_type
        {
            get
            {
                return this.job_typeField;
            }
            set
            {
                this.job_typeField = value;
            }
        }
    }

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

        private object address1Field;

        private object cityField;

        private object display_cityField;

        private object countryField;

        /// <remarks/>
        public object address1
        {
            get
            {
                return this.address1Field;
            }
            set
            {
                this.address1Field = value;
            }
        }

        /// <remarks/>
        public object city
        {
            get
            {
                return this.cityField;
            }
            set
            {
                this.cityField = value;
            }
        }

        /// <remarks/>
        public object display_city
        {
            get
            {
                return this.display_cityField;
            }
            set
            {
                this.display_cityField = value;
            }
        }

        /// <remarks/>
        public object country
        {
            get
            {
                return this.countryField;
            }
            set
            {
                this.countryField = value;
            }
        }
    }


    class Program
    {

        static void Main(string[] args)
        {
            var xml = @"<requisitions>
    <requisition>
        <start_date></start_date>
        <end_date></end_date>
        <title></title>
        <precise_location>
            <address1/>
            <city></city>
            <display_city/>
            <country></country>
        </precise_location>
        <contract_type></contract_type>
        <experience></experience>
        <job_type></job_type>
    </requisition>
    <requisition>
        <start_date></start_date>
        <end_date></end_date>
        <title></title>
        <precise_location>
            <address1/>
            <city></city>
            <display_city/>
            <country></country>
        </precise_location>
        <contract_type></contract_type>
        <experience></experience>
        <job_type></job_type>
    </requisition>
</requisitions>
";
            using (var reader = new StringReader(xml))
            {
                var deserializer = new XmlSerializer(typeof(requisitions), new XmlRootAttribute("requisitions"));

                var r = (requisitions)deserializer.Deserialize(reader);
            }
            Console.WriteLine("Hello World!");
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM