C# 如何在C语言中实现自定义XML序列化#

C# 如何在C语言中实现自定义XML序列化#,c#,xml-serialization,datacontractserializer,C#,Xml Serialization,Datacontractserializer,下面是我的XML文件 <Employee> <FirstName>#{FirstName}#</FirstName> <LastName>#{LastName}#</LastName> <DOB>#{DOB}#</DOB> <Address>#{Address}#</Address> <Transcation> <M

下面是我的XML文件

<Employee>
    <FirstName>#{FirstName}#</FirstName>
    <LastName>#{LastName}#</LastName>
    <DOB>#{DOB}#</DOB>
    <Address>#{Address}#</Address>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>
    <Transcation>
        <Month>#{Month}#</Month>
        <Amount>#{Amount}#</Amount>
    </Transcation>

#{FirstName}#
#{LastName}#
#{DOB}#
#{地址}#
#{Month}#
#{Amount}#
#{Month}#
#{Amount}#
#{Month}#
#{Amount}#
#{Month}#
#{Amount}#

我的可序列化类是

[XmlRoot("Employee"), Serializable]    
 public class Employee    
 {    
     [XmlAnyElement]
      public List<XmlElement> EmployeeDetails    { get; set; }
 }
[XmlRoot(“员工”),可序列化]
公营雇员
{    
[xmlanyement]
公共列表EmployeeDetails{get;set;}
}
但我想得到这样的东西

[XmlRoot("Employee"), Serializable]    
public class Employee    
{    
 [XmlAnyElement]
 public List<XmlElement> EmployeeDetails    { get; set; }

 [XmlElement("Transcation")]
 public List<Transcation> Transcations { get; set; }

}

public class Transcation
{
  [XmlElement("Month")]
    public string Month{ get; set; }
  [XmlElement("Amount")]
    public string Amount{ get; set; }

}
在我的员工详细资料中,我应该只序列化名字、姓氏、出生日期、地址等,。。。。我应该在一个单独的类中获取事务列表,该类包含月份和金额作为可序列化元素

像这样的

[XmlRoot("Employee"), Serializable]    
public class Employee    
{    
 [XmlAnyElement]
 public List<XmlElement> EmployeeDetails    { get; set; }

 [XmlElement("Transcation")]
 public List<Transcation> Transcations { get; set; }

}

public class Transcation
{
  [XmlElement("Month")]
    public string Month{ get; set; }
  [XmlElement("Amount")]
    public string Amount{ get; set; }

}
[XmlRoot(“员工”),可序列化]
公营雇员
{    
[xmlanyement]
公共列表EmployeeDetails{get;set;}
[XmlElement(“转换”)]
公共列表事务{get;set;}
}
公共类转换
{
[XmlElement(“月份”)]
公共字符串月份{get;set;}
[XmlElement(“金额”)]
公共字符串金额{get;set;}
}

我该怎么做

如果希望完全控制XML的外观,可以创建自己的XMLDocument并手动添加节点/元素。更多的工作,但是您可以对XML的外观进行微调。

假设您不能修改XML,您的类应该如下所示。可以使用XmlAnyElement,但可能需要将其设置为对象数组。因此,您的代码应该如下所示:

[XmlRoot("Employee"), Serializable]
public class Employee
{
    [XmlAnyElement]
    public XmlElement [] EmployeeDetails { get; set; }

    [XmlElement("Transcation")]
    public List<Transaction> Transcations { get; set; }
}
我建议您指定属性,这样可以更容易地查找特定的值。但这取决于您对xml模式的了解

[XmlRoot(ElementName="Employee")]    
public class Employee    
{    
  [XmlElement(ElementName="FirstName")]    
  public string FirstName {get;set;}

  [XmlElement(ElementName="LastName")]
  public string LastName {get;set;}

  [XmlElement(ElementName="DOB")]
  public DateTime DOB {get;set;}

  [XmlElement(ElementName="Address")]
  public string Address {get;set;}

  [XmlElement(ElementName="Transaction")]
  public List<Transaction> Transaction {get;set;}
}


[XmlRoot(ElementName="Transaction")]
public class Transaction
{

  [XmlElement(ElementName="Month")]
  public int Month {get;set;}

  [XmlElement(ElementName="Amount")]
  public int Amount {get;set;}
}
[XmlRoot(ElementName=“Employee”)]
公营雇员
{    
[xmlement(ElementName=“FirstName”)]
公共字符串名{get;set;}
[xmlement(ElementName=“LastName”)]
公共字符串LastName{get;set;}
[xmlement(ElementName=“DOB”)]
公共日期时间DOB{get;set;}
[xmlement(ElementName=“Address”)]
公共字符串地址{get;set;}
[xmlement(ElementName=“交易”)]
公共列表事务{get;set;}
}
[XmlRoot(ElementName=“事务”)]
公共类事务
{
[xmlement(ElementName=“Month”)]
公共整数月{get;set;}
[xmlement(ElementName=“Amount”)]
公共整数金额{get;set;}
}