尝试在 C# 中反序列化 XML

Trying to deserialization XML in C#

你好,我正在执行 XML 反序列化 我收到此错误:"There was an error reflecting field 'message'"

我认为这是因为元素不必在 XML.. 但不太确定。

附上我的代码

XML代码:

 <DataStructure>
 <message type="01">
 <dataBytes position="0" description="Response description" typeOfByte="enum">
  <enum val="00" description="Message Legal" />
  <enum val="01" description="Message not Recognized" />
  <enum val="02" description="Message Data Invalid" />
  <enum val="03" description="Unused" />
  <enum val="04" description="Buffer Full" />
  <enum val="05" description="Unused" />
  <enum val="06" description="Notification Error" />
  <enum val="07" description="One-Way Network" />
  <enum val="08" description="Message Length Error" />    
  <enum val="09" description="Network Unavailable" />
  <enum val="0A" description="Node Busy" />
</dataBytes>

 <dataBytes position="1" description="Response data" typeOfByte="enum">
  <enum val="00" description="Message Legal" />
  <enum val="01" description="Message not Recognized" />
  <enum val="02" description="Message Data Invalid" />
  <enum val="03" description="Unused" />
  <enum val="04" description="Buffer Full" />
  <enum val="05" description="Unused" />
  <enum val="06" description="Notification Error"/>
  <enum val="07" description="One-Way Network" />
  <enum val="08" description="Message Length Error" />
  <enum val="09" description="Network Unavailable" />    
  <enum val="0A" description="Node Busy" />
  </dataBytes>
 </message>


<message type="30">    
<dataBytes position="0" description="Node Short ID" typeOfByte="hex">   </dataBytes>
<dataBytes position="1" description="Node Short ID" typeOfByte="bits">      
    <bit position ="0-2" bitval="" description="Notification Period’ "></bit>
    <bit position ="3" bitval="" description ="Listening Mode" ></bit>
    <bit position ="4-5" typebit="enum" >
      <Enum val="00" description="NA" ></Enum>
      <Enum val="01" description="Poor" ></Enum>
      <Enum val="10" description="Good" ></Enum>
      <Enum val="11" description="Strong" ></Enum>
    </bit>
    <bit poistion ="6-7" description="Reserved"></bit>            
 </dataBytes>
 </message> 
</DataStructure>

C#代码:

    private string readFFDDataStructureXML()
    {
        string OK = ""; //the error is exits

        //perform serialazation for the XML
        try
        {

            string baseDirectory = @"C:\Users\brosenfeld\Perforce\brosenfeld_TA7BarakRo_4075\System\Integration\Tools\Panalyzer\FFDProtocol\DataStructure.xml"; //get current directory
            using (FileStream stream = new FileStream(baseDirectory, FileMode.Open))
            {
                XmlSerializer XML = new XmlSerializer(typeof(DataStructurecs));
                _XMLFFDData = (DataStructurecs)XML.Deserialize(stream);
            }
        }
        //if there is an error
        catch (Exception err)
        {
            if (err.InnerException == null)
            {
                OK = err.Message;
            }

            else
            {
                OK = err.Message + "\n\n" + err.InnerException;
            }
        }

        return OK;

    }







[XmlRoot("DataStructure")]
public class DataStructurecs
{

     [XmlElement("message")]
     public List<Message>  message; 

    public DataStructurecs()
    {
        message = new List<Message> ();
    }



    public class Message
    {
        [XmlAttribute("type")]
        public string type;

        [XmlElement("dataBytes")]
        public List<dataBytes> DataBytes;

        public Message ()
        {
            type="";
            DataBytes = new List<dataBytes>();
        }


        public class dataBytes
        {
            [XmlAttribute("position")]
            public string position;

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

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

            [XmlElement("bit")]
            public Bits bits;

            [XmlArrayItem("enum",(typeof (Enum)),IsNullable=trueQ)]
            public List<Enum> ENUM;


            public  dataBytes ()
            {
                position = "";
                description = "";
                typeOfByte = "";
                bits = new Bits();
                ENUM = new List<Enum>();

            }


            public class Enum
            {
                [XmlAttribute("val")]
                public string val;

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

                public Enum()
                {
                    val = "";
                    description = "";
                }
            }


            public class Bits
            {
                [XmlAttribute("position")]
                public string position;

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


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


                [XmlElement("Enum")]
                public List<Enum> BitsEnum;

                public Bits()
                {
                    position = "";
                    bitval = "";
                    description = "";
                    BitsEnum = new List<Enum>();
                }


                public class Enum
                {
                    [XmlAttribute("val")]
                    public string val;

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

                    public Enum()
                    {
                        val = "";
                        description = "";
                    }
                }
            }


        }
    }




}

你必须改变你的数据结构这是正确的:

[XmlRoot("DataStructure")]
public class DataStructurecs
{

    [XmlElement("message")]
    public List<Message> Message { get; set; }
}

public class Message
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlElement("dataBytes")]
    public List<DataBytes> DataBytes { get; set; }
}

public class DataBytes
{
    [XmlAttribute("position")]
    public string Position { get; set; }

    [XmlAttribute("description")]
    public string Description { get; set; }

    [XmlAttribute("typeOfByte")]
    public string TypeOfByte { get; set; }

    [XmlElement("bit")]
    public Bits Bits { get; set; }

    [XmlElement("enum")]
    public List<Enum> Enum { get; set; }
}

public class Enum
{
    [XmlAttribute("val")]
    public string Val { get; set; }

    [XmlAttribute("description")]
    public string Description { get; set; }
}


public class Bits
{
    [XmlAttribute("position")]
    public string Position { get; set; }

    [XmlAttribute("bitval")]
    public string Bitval { get; set; }


    [XmlAttribute("description")]
    public string Description { get; set; }


    [XmlElement("Enum")]
    public List<Enums> BitsEnum { get; set; }
}

public class Enums
{
    [XmlAttribute("val")]
    public string Val { get; set; }

    [XmlAttribute("description")]
    public string Description { get; set; }

}