2010-10-21 80 views
0

我有一個簡單的XML節點,我需要重新XML序列化屬性和文本

<Division ID="123">Division Name</Division> 

但是,當我創建該類的

public class Division 
{ 
    [XmlAttribute] 
    public string Id { get; set; } 

    [XmlText] 
    public string Description { get; set; } 
} 

我得到

<Division>Division Name</Division> 

的Id消失。

我該如何做這項工作?

+0

你怎麼讀呢? – Nix 2010-10-21 19:28:26

回答

1

Works on my machine


Division d = new Division() 
{ 
    Id = "1", 
    Description = "Description" 
}; 

FileStream fs = new FileStream("test.txt", FileMode.Create); 
TextWriter writer = new StreamWriter(fs, new UTF8Encoding()); 


XmlSerializer xs = new XmlSerializer(typeof(Division)); 
xs.Serialize(writer, d); 
writer.Close(); 

<?xml version="1.0" encoding="utf-8"?> 
<Division xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" Id="1">Description</Division> 
+0

我之後在剝離它們的代碼中遇到了一個錯誤...... *嘆息* – CaffGeek 2010-10-21 19:33:37

+0

星期五幾乎在這裏......你會沒事的.. – Nix 2010-10-21 19:35:55