我想将我的枚举值序列化为一个int,但我只得到名称。
原文链接:https://www.f2er.com/xml/294014.html这里是我的(样本)类和枚举:
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(到另一个问题)似乎表明枚举应该序列化为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>