简体   繁体   English

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

[英]error while Deserialize XML using C#

I am trying to Deserialize an XML object 我正在尝试反序列化XML对象

however I get an error while tried to deserialize "There was an error reflecting field 'Tabels' 但是在尝试反序列化时出现错误“反映了字段'Tabels'的错误

I am looking at object Tabels but I could not found the problem 我正在查看对象Tabels,但找不到问题

attaching code and XML: 附加代码和XML:

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# code: 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 = "";
                }
            }
         }

     }
    }
  }

Tabels property of class TLVStructure should be List<Table> instead of List<Tables> . TLVStructure类的Tabels属性应该是List<Table>而不是List<Tables> So if you replace following line 因此,如果您替换以下行

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

with

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

It should work. 它应该工作。

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

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