反序列化失败,即使使用与序列化时间和有效外观 xml 相同的类型也是如此



如果有人能指出我的客户经理和我的员工经理之间的区别,这会阻止客户正确反序列化,那就太好了。

我用我的程序和序列化程序创建这个 xml,并调用:

XmlSerializer bf = new XmlSerializer(typeof(ProjectConfiguration));
<?xml version="1.0"?>
<ProjectConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Title />
  <UtcLastEdited>0001-01-01T00:00:00</UtcLastEdited>
  <UtcLastChange>0001-01-01T00:00:00</UtcLastChange>
  <customerManager>
    <list>
      <Customer>
        <Firstname>Hans </Firstname>
        <LastName>meser</LastName>
        <ID>1</ID>
        <Contry>deutschland</Contry>
        <Town>berlin</Town>
        <Street>poststraße 2</Street>
      </Customer>
    </list>
  </customerManager>
  <employeeManager>
    <list>
      <Employee>
        <Firstname>hans </Firstname>
        <LastName>Meyer</LastName>
        <ID>1</ID>
        <Contry>Deutschland</Contry>
        <Town>Hamburg</Town>
        <Street>Bahofstraße 4</Street>
      </Employee>
    </list>
  </employeeManager>
  <Projects>
    <Project>
      <ID>1</ID>
      <Title>NewProjekt</Title>
      <StartDate>2018-03-14T21:49:58.5540372+01:00</StartDate>
      <Enddate>2018-03-29T21:49:58</Enddate>
      <ListOfProjectActions>
        <ProjectAction>
          <Title>Implemtierung</Title>
          <Day>2</Day>
          <AsstimatedTime />
          <Employee>1</Employee>
          <Projektwork>Implementierung</Projektwork>
          <ID>0</ID>
        </ProjectAction>
      </ListOfProjectActions>
      <Description>aweeqweqe</Description>
      <EmployeeToProject>
        <int>1</int>
      </EmployeeToProject>
      <Customer>0</Customer>
      <ProjektLeader>0</ProjektLeader>
    </Project>
  </Projects>
</ProjectConfiguration>

但是当我用相同类型反序列化时

XmlSerializer bf = new XmlSerializer(typeof(ProjectConfiguration));
ProjectConfiguration obj = bf.Deserialize(file) as ProjectConfiguration;

它以某种方式无法创建有效的CustomerManager即使它与几乎相同的构建Employeemanager配合良好

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
//using Projektverwaltung.Interfaces;
namespace Projektverwaltung.Klassen
{
    [Serializable()]
    public class CustomerManager
    {
        public CustomerManager()
        {
        }
        [XmlIgnore]
        EventHandler m_EditHandle = new EventHandler(DefaultHandle);
        [XmlIgnore] 
        Dictionary<int, Customer> m_CustomerList = new       Dictionary<int, Customer>();
        [XmlIgnore] 
        public Dictionary<int, Customer> CustomerList { get { return m_CustomerList; } set { m_CustomerList = value; } }
        List<Customer> listtemp = new List<Customer>();
        public List<Customer> list
        {
            get
            {
                try
                {
                    listtemp.Clear();
                    foreach (var item in CustomerList.Values)
                    {
                        listtemp.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    Program.Log(ex);
                }
                return listtemp;
            }
            set
            {
                listtemp = value;
            }
        }               
        private static void DefaultHandle(object sender, EventArgs e) { }
        [XmlIgnore] 
        public EventHandler EditCostumerEvent { get { return m_EditHandle; } set { m_EditHandle = value; } }
        public int IncrementID()
        {
            int defaultindex = 0;
            try
            {
                foreach (var item in CostumerList.Keys)
                {
                    defaultindex = defaultindex < item ? item : defaultindex;
                }
            }
            catch (Exception ex)
            {
                Program.Log(ex);
            }
            return defaultindex + 1;
        }
    }
}

但是员工经理几乎是一样的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
// using Projektverwaltung.Interfaces;
namespace Projektverwaltung.Klassen
{
    [Serializable()]
    public class EmployeeManager// : IEmployeeManager
    {
        [XmlIgnore]
        EventHandler m_DeleteHandle = new EventHandler(DefaultHandle);
        [XmlIgnore]
        EventHandler m_EditHandle = new EventHandler(DefaultHandle);
        [XmlIgnore]
        Dictionary<int, Employee> m_EmployeeList = new Dictionary<int, Employee>();
        List<int> m_usedIds = new List<int>();
        private static void DefaultHandle(object sender, EventArgs e) { }
        [XmlIgnore]
        public Dictionary<int, Employee> ListofIEmployee { get { return m_EmployeeList; }set { m_EmployeeList = value; } }
        [XmlIgnore]
        public EventHandler DeleteOfEmployee { get { return m_DeleteHandle; } set { m_DeleteHandle = value; } }
        [XmlIgnore]
        public EventHandler EditOfEmployee { get { return m_EditHandle; } set { m_EditHandle = value; } }
        public EmployeeManager()
        {
        }
        public int IncrementID()
        {
            int defaultindex = 0;
            try
            {
                foreach (var item in ListofIEmployee.Keys)
                {
                    defaultindex = defaultindex < item ? item : defaultindex;
                }
                foreach (var item in m_usedIds)
                {
                    defaultindex = defaultindex < item ? item : defaultindex;
                }
            }
            catch (Exception ex)
            {
                Program.Log(ex);
            }

            return defaultindex + 1;
        }
        public void DeleteEmployee(Employee employee)
        {
            try
            {
                if (ListofIEmployee.Remove(employee.ID))
                {
                    m_usedIds.Add(employee.ID);
                    DeleteOfEmployee.Invoke(employee, new EventArgs());
                }
                else
                {
                    // could not delete employeee
                }
            }
            catch (Exception ex )
            {
                Program.Log(ex);
            }

        }
        public void EditEmployee(Employee employee)
        {
            try
            {
                m_EditHandle.Invoke(employee, new EventArgs());
            }
            catch (Exception ex)
            {
                Program.Log(ex);
            }
        }
        List<Employee> listtemp = new List<Employee>();
        public List<Employee> list
        {
            get
            {
                try
                {
                    foreach (var item in ListofIEmployee.Values)
                    {
                        listtemp.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    Program.Log(ex);
                }
                return listtemp;
            }
            set
            {
                listtemp = value;
            }
        }
    }
}

这里的类是请求反序列化的类型

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Projektverwaltung.Interfaces;
using System.Windows.Forms;

namespace Projektverwaltung.Klassen
{
    [Serializable()]
    public class ProjectConfiguration //: ProjectConfiguration
    {
        List<Project> m_list;
        CostumerManager m_costumerManager;
        EmployeeManager m_employeeManager;
        public string Title { get; set;}
        public DateTime UtcLastEdited { get ; set ;}
        public DateTime UtcLastChange { get ; set ;}
        public ProjectConfiguration (List<Project> list,CostumerManager costumerManger,EmployeeManager employeeManagercar ,string strTitle,IWin32Window owner)
        {
            Title = strTitle;
            m_list = list;
            costumerManager = costumerManger;
            employeeManager = employeeManagercar;
        }
        public ProjectConfiguration()
        {
        }
        public CostumerManager costumerManager
        {
            get
            {
               return  m_costumerManager;
            }
            set
            {
                m_costumerManager = value;
            }
        }
        public EmployeeManager employeeManager
        {
            get
            {
                return m_employeeManager;
            }
            set
            {
                m_employeeManager = value;
            }
        }
        public List<Project> Projects { get { return m_list; }set { m_list = value; } }
    }
}

使用编辑后的名称进行编辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Projektverwaltung.Interfaces;
namespace Projektverwaltung.Klassen
{
    [Serializable()]
   public class Customer //: ICostumer
    {
        string m_strFirstName = string.Empty;
        string m_strLastName = string.Empty;
        string m_strContry = string.Empty;
        string m_strStreet = string.Empty;
        string m_strTown = string.Empty;
        int m_nId = 0;
        public Customer()
        {
        }
        public string Firstname { get {return m_strFirstName;} set { m_strFirstName = value; }}
        public string LastName { get {return m_strLastName;} set { m_strLastName=value; }}
        public int ID { get {return m_nId;} set {m_nId=value; }}
        public string Contry { get { return m_strContry;} set {m_strContry = value; }}
        public string Town { get {return  m_strTown;} set {m_strTown=value;} }
        public string Street { get {return m_strStreet;} set {m_strStreet=value; }}
        public string CombindedName { get{return string.Format("Kunde: {0} {1}", LastName, Firstname); } }
    }
}

我试图制作一个列表,因为 diconary 不能很好地序列化 临时列表只是一个在每次保存时被清空的列表,因此乘以保存使加载时不会冗余客户列表以 id 作为键写入字典

好的,

很明显它与退出Chek结合使用解决了问题,即使我不检查原因。 感谢马克指出 S 有点毫无意义

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
//using Projektverwaltung.Interfaces;
namespace Projektverwaltung.Klassen
{
    [Serializable()]
    public class CustomerManager//: IXmlSerializable
    {
        public CustomerManager()
        {
        }
        [XmlIgnore]
        EventHandler m_EditHandle = new EventHandler(DefaultHandle);
        [XmlIgnore] Dictionary<int, Customer> m_CostumerList = new Dictionary<int, Customer>();
        [XmlIgnore] public Dictionary<int, Customer> CostumerList { get { return m_CostumerList; } set { m_CostumerList = value; } }

       [XmlArray(ElementName = "CostumerList",IsNullable =true)]
        List<Customer> listtemp = new List<Customer>();
        public List<Customer> list
        {
            get
            {
                try
                {
                    foreach (var item in CostumerList.Values)
                    {
                        if (!listtemp.Exists(match=>match.ID==item.ID))
                        {
                            listtemp.Add(item);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Program.Log(ex);
                }

                return listtemp;
            }
            set
            {
            }
        }               

        private static void DefaultHandle(object sender, EventArgs e) { }


        [XmlIgnore] public EventHandler EditCostumerEvent { get { return m_EditHandle; } set { m_EditHandle = value; } }

        public int IncrementID()
        {
            int defaultindex = 0;
            try
            {
                foreach (var item in CostumerList.Keys)
                {
                    defaultindex = defaultindex < item ? item : defaultindex;
                }
            }
            catch (Exception ex)
            {
                Program.Log(ex);
            }
            return defaultindex + 1;
        }

    }
}

最新更新