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

时间:2012-11-21 13:45:53

标签: c# xml-serialization datacontractserializer

以下是我的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,....我应该在一个单独的类中获得Transcation列表,其中包含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; }

}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

假设您无法修改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;}
}

答案 1 :(得分:0)

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