在 C# 中反序列化 XML 文件

Deserialize XML file in C#

集成开发环境:微软 Visual Studio 2010 语言 : C#, XML

我正在尝试将 XML 文件反序列化为对象,但它在 XML 文档 (5,4) 中抛出错误。有什么建议吗?

这是我试过的代码...

   public void ReadXMLtblFieldingDetails(string xmlFileXDetails)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(tblXDetails));
            TextReader reader = new StreamReader(xmlXDetails);
            object obj = serializer.Deserialize(reader);
            tblXDetails XmlData = (tblXDetails)obj;
            BusinessObjectManager.Add(XmlData); 

        }

这是我要反序列化的XML文档...

<?xml version="1.0"?>
<tblXDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <RowModified>0001-01-01T00:00:00</RowModified>
  <MachineID>6A7FEBFDCEFC4DSSDAXX</MachineID>      
  <fielder1_x_axis>0</fielder1_x_axis>
  <fielder1_y_axis>0</fielder1_y_axis>
  <fielder2_x_axis>0</fielder2_x_axis>
  <fielder2_y_axis>0</fielder2_y_axis>
  <fielder3_x_axis>0</fielder3_x_axis>
  <fielder3_y_axis>0</fielder3_y_axis>
  <fielder4_x_axis>0</fielder4_x_axis>
  <fielder4_y_axis>0</fielder4_y_axis>
  <fielder5_x_axis>0</fielder5_x_axis>
  <fielder5_y_axis>0</fielder5_y_axis>
  <fielder6_x_axis>0</fielder6_x_axis>
  <fielder6_y_axis>0</fielder6_y_axis>
  <fielder7_x_axis>0</fielder7_x_axis>
  <fielder7_y_axis>0</fielder7_y_axis>
  <fielder8_x_axis>0</fielder8_x_axis>
  <fielder8_y_axis>0</fielder8_y_axis>
  <fielder9_x_axis>0</fielder9_x_axis>
  <fielder9_y_axis>0</fielder9_y_axis>
</tblxDetails>

这是错误:XML 文档 (5,4) 中的错误 // 用户代码未处理无效操作异常

tblXDetails 类:

public class tblXDetails
    {
        decimal _MachineID;
        float _fielder1_x_axis;
        float _fielder1_y_axis;
        float _fielder2_x_axis;
        float _fielder2_y_axis;
        float _fielder3_x_axis;
        float _fielder3_y_axis;
        float _fielder4_x_axis;
        float _fielder4_y_axis;
        float _fielder5_x_axis;
        float _fielder5_y_axis;
        float _fielder6_x_axis;
        float _fielder6_y_axis;
        float _fielder7_x_axis;
        float _fielder7_y_axis;
        float _fielder8_x_axis;
        float _fielder8_y_axis;
        float _fielder9_x_axis;
        float _fielder9_y_axis;
        public decimal MachineID
        {
            get { return _MachineID; }
            set { _MachineID = value; }
        }

        public float fielder1_x_axis
        {
            get { return _fielder1_x_axis; }
            set { _fielder1_x_axis = value; }
        }
        public float fielder1_y_axis
        {
            get { return _fielder1_y_axis; }
            set { _fielder1_y_axis = value; }
        }

        public float fielder2_x_axis
        {
            get { return _fielder2_x_axis; }
            set { _fielder2_x_axis = value; }
        }
        public float fielder2_y_axis
        {
            get { return _fielder2_y_axis; }
            set { _fielder2_y_axis = value; }
        }

        public float fielder3_x_axis
        {
            get { return _fielder3_x_axis; }
            set { _fielder3_x_axis = value; }
        }
        public float fielder3_y_axis
        {
            get { return _fielder3_y_axis; }
            set { _fielder3_y_axis = value; }
        }

        public float fielder4_x_axis
        {
            get { return _fielder4_x_axis; }
            set { _fielder4_x_axis = value; }
        }
        public float fielder4_y_axis
        {
            get { return _fielder4_y_axis; }
            set { _fielder4_y_axis = value; }
        }


        public float fielder5_x_axis
        {
            get { return _fielder5_x_axis; }
            set { _fielder5_x_axis = value; }
        }
        public float fielder5_y_axis
        {
            get { return _fielder5_y_axis; }
            set { _fielder5_y_axis = value; }
        }

        public float fielder6_x_axis
        {
            get { return _fielder6_x_axis; }
            set { _fielder6_x_axis = value; }
        }
        public float fielder6_y_axis
        {
            get { return _fielder6_y_axis; }
            set { _fielder6_y_axis = value; }
        }

        public float fielder7_x_axis
        {
            get { return _fielder7_x_axis; }
            set { _fielder7_x_axis = value; }
        }
        public float fielder7_y_axis
        {
            get { return _fielder7_y_axis; }
            set { _fielder7_y_axis = value; }
        }

        public float fielder8_x_axis
        {
            get { return _fielder8_x_axis; }
            set { _fielder8_x_axis = value; }
        }
        public float fielder8_y_axis
        {
            get { return _fielder8_y_axis; }
            set { _fielder8_y_axis = value; }
        }

        public float fielder9_x_axis
        {
            get { return _fielder9_x_axis; }
            set { _fielder9_x_axis = value; }
        }
        public float fielder9_y_axis
        {
            get { return _fielder9_y_axis; }
            set { _fielder9_y_axis = value; }
        }

MachineID 必须是 decimal(根据您的 class 定义),但在 xml 中提供了不兼容的值:6A7FEBFDCEFC4DSSDAXX。如果在 xml 中将其设置为 1,序列化就会通过。恐怕您需要编写自定义(反)序列化程序或更改 MachineID 的类型(如果可能)。