简体   繁体   English

c#XML反序列化时间字符串列表以列出DateTime对象

[英]c# XML deserialize list of time string to list DateTime bject

I'm having some trouble wrapping my head around this one. 我在缠头时遇到了一些麻烦。 Im returning a bunch of xml from an API call (which i have no control over).The data looks like this but with many more entries. 我从API调用返回了一堆xml(我无法控制)。数据看起来像这样,但有更多条目。

`<time>10:00:00</time>
 <go>true</go>
 <time>10:30:00</time>
 <go>false</go>
`

I can deserialize it fine into a list two lists of strings List<string> time and list<string> go 我可以将其反序列化为一个列表,其中两个列表为字符串List<string> time and list<string> go

However i really need that time to be deserialized into a datetime object. 但是,我确实需要将该时间反序列化为datetime对象。 Right now i have the following working but only for a single instance but not for a list. 现在我有以下工作,但仅适用于单个实例,而不适用于列表。 Im having trouble with the getter and setter no doubt 我毫无疑问会遇到吸气剂和塞特剂

[XmlIgnore]
public List<DateTime> DoNotSerialize { get; set; }
[XmlElement("time")]
public List<string> time
{
     get { return DoNotSerialize.ToString("HH:MM:SS") }
     set { DoNotSerialize = DateTime.Parse(value); }
}

Try using this: 尝试使用此:

[XmlIgnore]
public List<DateTime> DoNotSerialize { get; set; }
[XmlElement("time")]        
public List<string> time
{
    get { return DoNotSerialize.Select(item => item.ToString("yyyy-MM-dd")).ToList(); }
    set { DoNotSerialize = value.Select(item => DateTime.Parse(item)).ToList(); }
}

(I had a similar problem days ago, but with serialization instead of deserialization. If you're interested, check my previous SO question ...) (几天前我遇到了类似的问题,但是使用序列化而不是反序列化。如果您有兴趣,请检查我之前的SO问题 ...)

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

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