[英]How do you use XMLSerialize for Enum typed properties in c#?
我有一個簡單的枚舉:
enum simple
{
one,
two,
three
};
我也有一個類型simple
的類。 我嘗試使用以下屬性進行裝飾: [XmlAttribute(DataType = "int")]
。 但是,當我嘗試使用XmlWriter
序列化它時它失敗了。
這樣做的正確方法是什么? 我是否必須標記枚舉本身以及屬性,如果是,請使用哪種數據類型?
串行化枚舉屬性應該沒有任何問題:
public enum Simple { one, two, three }
public class Foo
{
public Simple Simple { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var writer = XmlWriter.Create(Console.OpenStandardOutput()))
{
var foo = new Foo
{
Simple = Simple.three
};
var serializer = new XmlSerializer(foo.GetType());
serializer.Serialize(writer, foo);
}
}
}
生產:
<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Simple>three</Simple>
</Foo>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.