使用 C# 反序列化 XML 时出错

error while Deserialize XML using C#

我正在尝试反序列化一个 XML 对象

但是我在尝试反序列化时遇到错误 “反映字段时出现错误 'Tabels'

我正在查看对象 Tabels 但我找不到问题

附加代码和XML:

XML:

<?xml version="1.0" encoding="utf-8" ?>
<DPR>

<TLV type_Description="Passive Motion" type_Value ="01"  workPer="Table" table_name="Detection Event" />

 <TLV type_Description="Passive Occupation" type_Value ="03"  workPer="Table"   table_name="Detection Event" />

 <TLV type_Description="Active Barrier" type_Value ="04"  workPer="Table"   table_name="Detection Event" />

  <TLV type_Description="Glass Break" type_Value ="06"  workPer="Table"  table_name="Detection Event" />


 <Tabels>
 <Tabel Name="Detection Event" Length="1">
  <val_byte Num="0" byte_type="enum" byte_Value="00"   value_description="Close" />
  <val_byte Num="0" byte_type="enum" byte_Value="01" value_description="Open"     />
    <val_byte Num="0" byte_type="enum" byte_Value="02" value_description="Violated" />
  <val_byte Num="0" byte_type="enum" byte_Value="03" value_description="Gross Attack" />
  <val_byte Num="0" byte_type="enum" byte_Value="04" value_description="Low Integration" />
</Tabel>

</Tabels>

</DPR>

C#代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Xml;
 using System.Xml.Serialization;

  namespace FFDProtocol
  {
  [XmlRoot("DPR")]
  public class TLVStructure
 {

    [XmlArray("TLV")]
    public List<TLV> LTLV;


    [XmlArray("Tabels")]
    public List<Tables> Tabels;


    public TLVStructure()
    {
        LTLV = new List<TLV> ();
        Tabels = new List<Tables>();
    }


    public class TLV         
    {
        [XmlAttribute("type_Description")]
        public string type_Description;
        [XmlAttribute("type_Value")]
        public string type_Value;

        [XmlAttribute("Length")]
        public string Length;


        [XmlAttribute("workPer")]
        public string workPer;

        [XmlElement("val_byte")]
        public List< val_byte> Val_byte;

        [XmlAttribute("table_name")]
        public string table_name;



        public TLV()
        {
            type_Description="";
            type_Value = "";
            Val_byte = new List<val_byte>();
            workPer = "";
            table_name = "";

        }

       public class val_byte 
        {


            [XmlAttribute("Num")]
            public string byteNum;
            [XmlAttribute("byte_Value")]
            public string byte_Value;
            [XmlAttribute("value_description")]
            public string value_description;            
            [XmlAttribute("byte_type")]
            public string byte_type;              
            [XmlAttribute("MCode")]


            public string MCode;
            public val_byte()
            {
                byteNum = "";
                byte_Value = "";
                value_description = "";
                byte_type = "";
                MCode = "";
            }
        }            

     }

    public class Tables
    {
        [XmlArray("Tabel")]
        public List<Tabel> LTabel;

        public Tables()
        {

            LTabel = new List<Tabel>();

        }

        public class Tabel
        {
            [XmlAttribute("Length")]
            public string Length;


            [XmlAttribute("Name")]
            public string Name;

            [XmlElement("val_byte")]
            public List<val_byte> LvalBytes;

            public Tabel()
            {
                Name = "";
                Length = "";
                LvalBytes = new List<val_byte>();
            }


            public class val_byte
            {


                [XmlAttribute("Num")]
                public string byteNum;
                [XmlAttribute("byte_Value")]
                public string byte_Value;
                [XmlAttribute("value_description")]
                public string value_description;
                [XmlAttribute("byte_type")]
                public string byte_type;
                [XmlAttribute("MCode")]


                public string MCode;
                public val_byte()
                {
                    byteNum = "";
                    byte_Value = "";
                    value_description = "";
                    byte_type = "";
                    MCode = "";
                }
            }
         }

     }
    }
  }

class TLVStructure 的标签 属性 应该是 List<Table> 而不是 List<Tables>。因此,如果您替换以下行

[XmlArray("Tabels")]
public List<Tables> Tabels;

[XmlArray("Tabels")]
public List<Table> Tabels;

应该可以。