我創建了一個可序列化的對象,我想將它序列化爲XML,然後再反序列化。我想要的是將該對象的一個屬性保存爲XML屬性。這裏是我的意思是:如何將對象的屬性序列化爲XML元素的屬性?
[Serializable]
public class ProgramInfo
{
public string Name { get; set; }
public Version Version { get; set; }
}
public class Version
{
public int Major { get; set; }
public int Minor { get; set; }
public int Build { get; set; }
}
我想的ProgramInfo保存到看起來像這樣的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<ProgramInfo Name="MyApp" Version="1.00.0000">
</ProgramInfo>
通知版本屬性,並以XML及其相應的屬性。我已經有解析器將字符串「1.00.0000」轉換爲有效的版本對象,反之亦然,但我不知道如何將它用於此XML序列化場景。
HTTP的可能重複:// stackoverflow.com/questions/11330643/serialize-property-as-xml-attribute-in-element – atomaras