C# 将 XML 反序列化为模型类错误 - <xmlns=""> 不是预期的

Posted

技术标签:

【中文标题】C# 将 XML 反序列化为模型类错误 - <xmlns=""> 不是预期的【英文标题】:C# Deserialize XML to Model Class error - <xmlns=""> Not expected 【发布时间】:2021-05-15 16:42:53 【问题描述】:

我正在尝试获取一个 XML 字符串并将其反序列化并将其绑定到我的 Model 类。但是我收到了这个错误:

System.InvalidOperationException : &lt;requisitions xmlns=''&gt; 不是预期的

这是我调用反序列化的 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);

我的 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>

我要反序列化的类是这样的:

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; 
    


解决方案

我使用 Visual Studio 的 Paste Special 功能来生成模型。但是我出错的地方是我复制到剪贴板的 XML 只有一个 Requisition 元素。因此,如果我将有多个申请,Visual Studio 并没有按照我需要的方式生成类。

【问题讨论】:

试试:typeof(JobsModel.requisition) 为什么有嵌套类?您的 实际 XML 是什么样的,是根元素 &lt;requisitions&gt;,复数,包含多个子元素吗? 我使用 Visual Studio 的“Paste Special”功能来生成 JobsModel 类。我在帖子中的 XML 实际上是错误的!我会更新。顶层是 ,其中包含多个 @CodeCaster 不知道为什么我有嵌套类。 Visual Studio 为我生成了这段代码。我所做的只是把它全部放在一个 JobsModel 类中。我可能做错了。 我不会阻止嵌套类。它只是根类需要匹配xml的根节点。我会将反序列化代码放在 JobsModel 类中,这样所有进行序列化的代码和类都在一个父类中。 【参考方案1】:

您不应该真正修改生成的类。如果需要,您可以重命名根对象。但是做一些小的改变来测试每一个。特殊粘贴 + 反序列化与该 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!");
        
    

【讨论】:

感谢您的回答。我又试了一次,没有弄乱生成的类,它像你说的那样工作。我从 URL 获取 XML,但不知道会有多少 对象。当我测试这个时,我只得到了前 2 个。 示例文档中只有 2 个。但它应该适用于任何数字。

以上是关于C# 将 XML 反序列化为模型类错误 - <xmlns=""> 不是预期的的主要内容,如果未能解决你的问题,请参考以下文章

将 XML 文件反序列化为 C# 类对象时出错

将 xml 反序列化为 c# 对象时,XML 文档 (2, 2) 出现错误

将xml反序列化为c#中的类时从XmlTextAttribute获取int值

如何将 XML 反序列化为 C# 中的对象? [复制]

C#将不同类型的xml数组反序列化为多个数组

如何在c#中将xml反序列化为列表