简体   繁体   English

XML反序列化

[英]Deserialization of XML

Trying to deserialize a XML log file. 尝试反序列化XML日志文件。 And cannot seem to get anything but Error in XML Document(0,0). 而且,除了XML Document(0,0)中的Error之外,似乎什么也没得到。 I am guessing it has something to do with my class but I cannot seem to find a solution. 我猜想这与我的课堂有关,但我似乎找不到解决方案。 I cannot change the XML formatting as this is a log file coming from a server (just simplified) 我无法更改XML格式,因为这是来自服务器的日志文件(只是简化了)

XML XML格式

<?xml version="1.0" encoding="utf-8"?>
<POSLog>
<Transaction>
  <RetailStoreID>1</RetailStoreID>
  <SequenceNumber>2</SequenceNumber>
</Transaction>
<Transaction>
  <RetailStoreID>1</RetailStoreID>
  <SequenceNumber>3</SequenceNumber>
</Transaction>
</POSLog>

Class

[Serializable()]
public class Transaction
{
    [XmlElement("RetailStoreID")]
    public string RetailStoreID { get; set; }

    [XmlElement("SequenceNumber")]
    public string SequenceNumber { get; set; }

}

[Serializable()]
[XmlRoot("POSLog")]
public class POSLog
{
    [XmlArray("POSLog")]
    [XmlArrayItem("Transaction", typeof(Transaction))]
    public Transaction[] Transaction { get; set; }
}

Deserialize code 反序列化代码

POSLog poslog = new POSLog();
string path = "POSLog.xml";
XmlSerializer serializer = new XmlSerializer(typeof(POSLog));
StreamReader reader = new StreamReader(path);
poslog = (POSLog)serializer.Deserialize(reader);

Found fix by switching from StreamReader to FileStream along with changes to the class. 通过从StreamReader切换到FileStream以及对类的更改找到了解决方法。 Changing encoding didn't seem to help when using the StreamReader. 使用StreamReader时,更改编码似乎没有帮助。

I cannot reproduce the problem you are seeing. 我无法重现您看到的问题。 However, there is an issue with the POSLog class -- it needs to be defined as follows: 但是, POSLog类存在问题-需要定义如下:

[Serializable()]
[XmlRoot("POSLog")]
public class POSLog
{
    [XmlElement("Transaction")]
    public Transaction[] Transaction { get; set; }
}   

Your XML has a root element <POSLog> containing a repeating sequence of <Transaction> elements. 您的XML具有根元素<POSLog>其中包含重复序列的<Transaction>元素。 [XmlElement("Transaction")] maps the array to just such a one-level repeating sequence. [XmlElement("Transaction")]将数组映射到这样的一级重复序列。

Example fiddle . 小提琴的例子。

Changing the Class to the follow above answer 将班级更改为上述答案

[Serializable()]
[XmlRoot("POSLog")]
public class POSLog
{
    [XmlElement("Transaction")]
    public Transaction[] Transaction { get; set; }
}   

Along with Changing the following StreamReader lines 以及更改以下StreamReader行

StreamReader reader = new StreamReader(path);
poslog = (POSLog)serializer.Deserialize(reader);

To this: 对此:

FileStream fs = new FileStream(path, FileMode.Open);
poslog = (POSLog)serializer.Deserialize(fs);

Fixed the issue I was having with the root element and I was able to deserialize the XML. 修复了root元素遇到的问题,并且我能够反序列化XML。 Thanks to dbc for help in getting me started solving a solution for the first time in a forum! 感谢dbc的帮助,让我第一次在论坛上开始解决解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM