如何反序列化为 List<String>使用 XmlSerializer [英] How to deserialize into a List<String> using the XmlSerializer
问题描述
我正在尝试将下面的 XML 反序列化为类,将 Components
反序列化为 List
,但不知道该怎么做.反序列化器适用于所有其他属性,但不适用于 Components
.有人知道怎么做吗?
I'm trying to deserialize the XML below into class, with the Components
deserialized into a List<string>
, but can't figure out how to do so. The deserializer is working fine for all the other properties, but not Components
. Anyone know how to do this?
<ArsAction>
<CustomerName>Joe Smith</CustomerName>
<LoginID>jdsmith</LoginID>
<TicketGroup>DMS</TicketGroup>
<Software>Visio 2007 Pro</Software>
<Components>
<Component>Component 1</Component>
<Component>Component 2</Component>
</Components>
<Bldg>887</Bldg>
<Room>1320p</Room>
</ArsAction>
推荐答案
添加这样的属性来保存组件列表:
Add a property like this to hold the list of Components:
[XmlArray()]
public List<Component> Components { get; set; }
抱歉我误读了.您想将其读入字符串集合.我只是在下面试过这个,它对你的样本有效.关键只是设置正确的xml序列化属性.
Sorry I misread that. You want to read it into a collection of strings. I just tried this below and it worked on your sample. The key is just to setup the correct xml serialization attributes.
public class ArsAction
{
[XmlArray]
[XmlArrayItem(ElementName="Component")]
public List<string> Components { get; set; }
}
这篇关于如何反序列化为 List<String>使用 XmlSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!