我正在尝试将下面的 XML 反序列化为类,使用 Components
反序列化为 List<string>
,但不知道该怎么做。反序列化器对所有其他属性工作正常,但不是 Components
.有人知道怎么做吗?
<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>
最佳答案
添加这样的属性来保存组件列表:
[XmlArray()]
public List<Component> Components { get; set; }
编辑:抱歉我看错了。你想把它读入一个字符串集合。我刚刚在下面尝试了这个并且它适用于您的示例。关键是设置正确的 xml 序列化属性。
public class ArsAction
{
[XmlArray]
[XmlArrayItem(ElementName="Component")]
public List<string> Components { get; set; }
}
关于c# - 如何使用 XmlSerializer 反序列化为 List<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1208643/