gpt4 book ai didi

c# - 反序列化不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 01:43:04 25 4
gpt4 key购买 nike

这是 xml 流:

<?xml version="1.0" encoding="utf-8" ?> 
<historydetails>
<taskEvent>
<eventtype>Transitions</eventtype>
<historyevent>Task moved</historyevent>
<details>From 'Requested' to 'In Validation'</details>
<author>NAme</author>
<entrydate>01 Jul 13, 11:34</entrydate>
<historyid>2620</historyid>
</taskEvent>
<taskEvent>
<eventtype>Updates</eventtype>
<historyevent>Subtask marked done</historyevent>
<details>Subtask: visualise status and versions</details>
<author>NAme2</author>
<entrydate>21 Jun 13, 10:16</entrydate>
<historyid>2588</historyid>
</taskEvent>
</historydetails>

相应的类如下所示:

public class historydetails
{
[XmlElement("taskEvent")]
List<taskEvent> eventList = new List<taskEvent>();
}

public class taskEvent
{
string eventtype { get; set; }
string historyevent { get; set; }
string details { get; set; }
string author { get; set; }
string entrydate { get; set; }
string historyid { get; set; }
}

反序列化xml的代码(字符串替换包含xml代码):

XmlSerializer deserializer = new XmlSerializer(typeof(historydetails));                              
object obj = obj = deserializer.Deserialize(stringToStream(replacement));
historydetails XmlData = (historydetails)obj;

方法stringToStream

private MemoryStream stringToStream(string input)
{
byte[] byteArray = Encoding.ASCII.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
return stream;
}

我得到的结果如下:对象 XmlData 已创建,并且有一个 taskEvents 列表。问题出在列表本身:它是空的...

最佳答案

你必须让成员公开

public class historydetails
{
[XmlElement("taskEvent")]
public List<taskEvent> eventList = new List<taskEvent>();
}

public class taskEvent
{
public string eventtype { get; set; }
public string historyevent { get; set; }
public string details { get; set; }
public string author { get; set; }
public string entrydate { get; set; }
public string historyid { get; set; }
}

关于c# - 反序列化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18017591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com