我想将我的enum-value序列化为一个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();
这个答案(针对另一个问题)似乎表明默认情况下枚举应序列化为ints,但似乎没有这样做。这是我的输出:
<?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”)]“属性,我已经能够将其序列化为值,但这似乎是错误的。