我正在编写一个从.xml文件反序列化为对象的方法。我有一个内部异常,即在xml文件中将无效字符串格式从字符串解析为Datetime(7,6):
<?xml version="1.0" encoding="utf-8"?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Table>1</Table>
<OrderInfo>
<OrderID>0</OrderID>
<OrderType>AtStore</OrderType>
<Time>22/12/2014 21:07:44</Time>
<RentStatus>Finish</RentStatus>
<CancelReason>-</CancelReason>
<Cashier>test14</Cashier>
<Session>1</Session>
</OrderInfo>
<CustomerInfo />
<OrderPayment>
<PaymentType>Cash</PaymentType>
<Total>258</Total>
<DiscountPercent>0</DiscountPercent>
<Discount>0</Discount>
<Payment>258</Payment>
<PaymentFromCash>258</PaymentFromCash>
<PaymentFromWallet>0</PaymentFromWallet>
<Receive>258</Receive>
<Exchange>0</Exchange>
</OrderPayment>
<OrderDetails>
<OrderDetail>
<ProductID>107</ProductID>
<Code>5</Code>
<ProductName>abc</ProductName>
<Quantity>1</Quantity>
<Price>258</Price>
<DiscountPercent>0</DiscountPercent>
<Discount>0</Discount>
<Total>258</Total>
<Payment>258</Payment>
<Note />
</OrderDetail>
</OrderDetails>
但是,我班上没有Datetime字段:
public class Order
{
public OrderInfo OrderInfo { get; set; }
public OrderPayment OrderPayment { get; set; }
public CustomerInfo CustomerInfo { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}
public class OrderInfo
{
public string OrderID { get; set; }
public string OrderOnlineID { get; set; }
public int OrderType { get; set; }
public string Time { get; set; }
public string Note { get; set; }
public int RentStatus { get; set; }
public string Cashier { get; set; }
public string Session { get; set; }
public string CancelReason { get; set; }
}
public class OrderPayment
{
public int PaymentType { get; set; }
public int Total { get; set; }
public int DiscountPercent { get; set; }
public int Discount { get; set; }
public int Payment { get; set; }
public int PaymentFromCash { get; set; }
public int PaymentFromWallet { get; set; }
public int Receive { get; set; }
public int Exchange { get; set; }
}
public class OrderDetail
{
public int ProductID { get; set; }
public string Code { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public int Price { get; set; }
public int DiscountPercent { get; set; }
public int Total { get; set; }
public int Discount { get; set; }
public int Payment { get; set; }
public OrderDetail()
{
DiscountPercent = 0;
Total = 0;
Discount = 0;
Payment = 0;
}
}
public class CustomerInfo
{
public string CustomerName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
}
这是我的反序列化代码:
var serializer = new XmlSerializer(typeof(Order));
Order order;
using (TextReader reader = new StreamReader(filePath))
{
order = (Order)serializer.Deserialize(reader); //the exception appears here
}
序列化时,我也使用字符串,而不是日期时间。所以我认为这只是将字符串序列化为xml并从xml反序列化为字符串。我无法理解异常发生的原因......
答案 0 :(得分:0)
我测试了你的XML文档,它有几个问题:
以下元素包含字符串数据,但类属性声明为数字类型:
<OrderType>AtStore</OrderType>
...
<RentStatus>Finish</RentStatus>
...
<PaymentType>Cash</PaymentType>
我猜这些应该是枚举?
此外,Order
元素未关闭,但我怀疑这只是将XML复制粘贴到问题中的问题。
修好这些东西后,它反序列化了。