2012-11-21 70 views
0

以下是我的XML文件。如何在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我應該序列只有名字,姓氏,出生日期,地址, ....我應該在一個單獨的類中包含Month和Amount作爲可序列化元素的Transcation List。

像這樣

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

} 

我怎樣才能做到這一點?

回答

0

假設你不能修改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;} 
} 
0

如果您想完全控制XML的外觀,您可以創建自己的XMLDocument並手動添加節點/元素。更多的工作,但你可以精確地調整XML的外觀。