23

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>
1
  • Please post the class you're trying to serialize and deserialize. Commented Jul 30, 2009 at 19:26

1 Answer 1

50

Add a property like this to hold the list of Components:

[XmlArray()]
public List<Component> Components { get; set; }

Edit: 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; }
}
2
  • [XmlArray] is optional if the property name is the same as tag name
    – Chris Xue
    Commented Dec 22, 2015 at 9:20
  • [XmlArrayItem(ElementName="Component")] can be shorten as [XmlArrayItem("Component")]. Tested on .Net 4.0
    – Chris Xue
    Commented Dec 22, 2015 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.