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

时间:2016-12-26 13:52:59

标签: c# xml xml-serialization

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

然而,在尝试反序列化时出现错误 "反映字段' 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 = "";
                }
            }
         }

     }
    }
  }

1 个答案:

答案 0 :(得分:3)

TLVStructure类的Tabels属性应为List<Table>而不是List<Tables>。所以,如果你更换以下行

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

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

它应该有用。