【问题标题】:Cannot deserialize to string无法反序列化为字符串
【发布时间】:2014-12-22 14:41:06
【问题描述】:

我正在编写一种从 .xml 文件反序列化为对象的方法。我有一个内部异常,即在 xml 文件中的 (7,6) 处从字符串解析为 Datetime 的无效字符串格式:

<?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>

但是,我的班级没有日期时间字段:

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 反序列化为字符串。我不明白为什么会发生异常...

【问题讨论】:

  • 你的Thread.CurrentThread.CurrentCulture 值是多少?
  • 在 xml 文件中从字符串解析为日期时间 (7,6) 的字符串格式无效。这个日期时间在哪里
  • 如果您收到错误 exception that invalid string format to parse from string to Datetime 我是 110% 确定您在某处使用了 Datetime 数据类型 ..field
  • 在序列化的时候,我也是用字符串,而不是日期时间。所以我认为这只是将字符串序列化为 xml 并将 xml 反序列化为字符串。我只是不知道为什么会出现 Datetime 异常。
  • 如果 DateTime 格式有错误,则必须解析值,例如:string s = "22/12/2014 21:07:44"; DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy HH:mm:ss",CultureInfo.InvariantCulture);

标签: c# xml datetime serialization


【解决方案1】:

我测试了你的 XML 文档,它有几个问题:

以下元素包含字符串数据,但类属性声明为数字类型:

<OrderType>AtStore</OrderType>
...
<RentStatus>Finish</RentStatus>
...
<PaymentType>Cash</PaymentType>

我猜这些应该是枚举?

另外,Order 元素没有关闭,但我怀疑这只是将 XML 复制粘贴到问题中的问题。

修复这些东西后,它反序列化了。

【讨论】:

  • 谢谢,我的代码运行良好。但是,我有点困惑为什么它会抛出日期时间格式异常。
  • 这很奇怪,在我的情况下它并没有抛出那个。我不太确定为什么会发生这种情况。
猜你喜欢
  • 2018-10-22
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多