我想将我的枚举值序列化为int,但我只得到这个名字。
这是我的(示例)类和枚举:
public class Request {
public RequestType request;
}
public enum RequestType
{
Booking = 1,
Confirmation = 2,
PreBooking = 4,
PreBookingConfirmation = 5,
BookingStatus = 6
}
代码(只是为了确保我做错了)
Request req = new Request();
req.request = RequestType.Confirmation;
XmlSerializer xml = new XmlSerializer(req.GetType());
StringWriter writer = new StringWriter();
xml.Serialize(writer, req);
textBox1.Text = writer.ToString();
This answer(对于另一个问题)似乎表明枚举应该序列化为默认值,但它似乎没有这样做。这是我的输出:
<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<request>Confirmation</request>
</Request>
我能够通过在每个值上加上“[XmlEnum(”X“)]”属性来序列化为值,但这似乎不对。
答案 0 :(得分:134)
最简单的方法是使用[XmlEnum]属性,如下所示:
[Serializable]
public enum EnumToSerialize
{
[XmlEnum("1")]
One = 1,
[XmlEnum("2")]
Two = 2
}
这将序列化为XML(比如父类是CustomClass),如下所示:
<CustomClass>
<EnumValue>2</EnumValue>
</CustomClass>
答案 1 :(得分:67)
大多数时候,人们想要名字,而不是整数。您可以为此目的添加垫片属性吗?
[XmlIgnore]
public MyEnum Foo {get;set;}
[XmlElement("Foo")]
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public int FooInt32 {
get {return (int)Foo;}
set {Foo = (MyEnum)value;}
}
或者您可以使用IXmlSerializable
,但这需要做很多工作。
答案 2 :(得分:13)
请参阅下面的完整示例控制台应用程序,以获得使用DataContractSerializer实现所需内容的有趣方法:
using System;
using System.IO;
using System.Runtime.Serialization;
namespace ConsoleApplication1
{
[DataContract(Namespace="petermcg.wordpress.com")]
public class Request
{
[DataMember(EmitDefaultValue = false)]
public RequestType request;
}
[DataContract(Namespace = "petermcg.wordpress.com")]
public enum RequestType
{
[EnumMember(Value = "1")]
Booking = 1,
[EnumMember(Value = "2")]
Confirmation = 2,
[EnumMember(Value = "4")]
PreBooking = 4,
[EnumMember(Value = "5")]
PreBookingConfirmation = 5,
[EnumMember(Value = "6")]
BookingStatus = 6
}
class Program
{
static void Main(string[] args)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Request));
// Create Request object
Request req = new Request();
req.request = RequestType.Confirmation;
// Serialize to File
using (FileStream fileStream = new FileStream("request.txt", FileMode.Create))
{
serializer.WriteObject(fileStream, req);
}
// Reset for testing
req = null;
// Deserialize from File
using (FileStream fileStream = new FileStream("request.txt", FileMode.Open))
{
req = serializer.ReadObject(fileStream) as Request;
}
// Writes True
Console.WriteLine(req.request == RequestType.Confirmation);
}
}
}
在调用WriteObject后,request.txt的内容如下:
<Request xmlns="petermcg.wordpress.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<request>2</request>
</Request>
您需要为DataContractSerializer引用System.Runtime.Serialization.dll程序集。
答案 3 :(得分:2)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<StackPanel x:Name="stackPanel">
</StackPanel>
</Window>
上述方法对我有用。
答案 4 :(得分:0)
由于您要为枚举选项分配显式非连续值,我假设您希望能够一次指定多个值(二进制标志),因此接受的答案是您唯一的选择。通过PreBooking | PreBookingConfirmation将有一个9的整数值,并且序列化程序将无法反序列化它,使用shim属性将其强制转换但是效果很好。或许你只是错过了3值:)
答案 5 :(得分:-1)
查看System.Enum类。 Parse方法将字符串或int表示转换为Enum对象,ToString方法将Enum对象转换为可以序列化的字符串。