在C#中反序列化XML文件

时间:2015-01-07 06:04:45

标签: c# xml xml-serialization xml-deserialization

IDE:Microsoft 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; }
        }

1 个答案:

答案 0 :(得分:2)

MachineID必须是decimal(根据您的班级定义),但在xml中提供了不兼容的值:6A7FEBFDCEFC4DSSDAXX。如果在序列化传递的xml中将其设置为1。我担心您需要编写自定义(de)序列化程序或更改MachineID的类型(如果可能)。