Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize into a List<String> using the XmlSerializer

Tags:

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> 
like image 870
Russ Clark Avatar asked Jul 30 '09 19:07

Russ Clark


People also ask

How does the XmlSerializer work C#?

The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

What is deserialize XML?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.

Is XmlSerializer thread safe?

4 Answers. Show activity on this post. This type is thread safe.


1 Answers

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; } } 
like image 59
Brian Ensink Avatar answered Sep 21 '22 06:09

Brian Ensink