即使使用与序列化时间和有效查找xml

时间:2018-03-14 21:23:15

标签: c# deserialization xml-serialization

如果有人可以指出我的客户经理和我的员工经理之间的不同之处会阻止客户正确反序列化,那会很棒。

我使用带有此调用的序列化程序的程序创建此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); } }
    }
}

我试图制作一个列表,因为diconarys不能很好地序列化临时列表只是一个列表,每个保存都会被清空所以多次保存make而不是redunant costumers加载列表被写入dictonarys,id为key

1 个答案:

答案 0 :(得分:0)

好的,显然它与它结合并且退出chek解决了问题,即使我没有检查原因。 thx marc指出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;
        }


    }
}