繁体   English   中英

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

[英]How to implement Custom XML Serialization in C#

下面是我的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>

我的可序列化类是

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

但是我想得到这样的东西

在我的EmployeeDetails中,我应该只序列化FirstName,LastName,DOB,Address .....,并且应该在包含Month和Amount作为serialiazable元素的单独类中获取事务处理列表。

像这样的东西

[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; }

}

我怎样才能做到这一点 ?

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

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

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

要反序列化,请执行以下操作:

    private void DeserializeObject(string filename)
    {
        XmlAnyElementAttribute myAnyElement = new XmlAnyElementAttribute();
        XmlAttributeOverrides xOverride = new XmlAttributeOverrides();
        XmlAttributes xAtts = new XmlAttributes();
        xAtts.XmlAnyElements.Add(myAnyElement);
        xOverride.Add(typeof(Employee), "EmployeeDetails", xAtts);

        XmlSerializer ser = new XmlSerializer(typeof(Employee), xOverride);

        FileStream fs = new FileStream(filename, FileMode.Open);
        var g = (Employee)ser.Deserialize(fs);
        fs.Close();

        Console.WriteLine(g.EmployeeDetails.Length);
        foreach (XmlElement xelement in g.EmployeeDetails)
        {
            Console.WriteLine(xelement.Name + ": " + xelement.InnerXml);
        }
    }

我建议改为指定属性,这将使查找特定值更加容易。 但这取决于您对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;}
}

如果要完全控制XML的外观,可以创建自己的XMLDocument并手动添加节点/元素。 需要做更多的工作,但是您可以精确地调整XML的外观。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM