Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing array from XML data (in ServiceStack)
    primarykey
    data
    text
    <p>I've got the following chunk of XML data:</p> <pre><code>&lt;ArrayOfRESTDataSource xmlns="http://SwitchKing.Common/Entities/RESTSimplified/2010/07" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;RESTDataSource&gt; &lt;Description&gt;MyTest&lt;/Description&gt; &lt;Enabled&gt;true&lt;/Enabled&gt; &lt;/RESTDataSource&gt; &lt;/ArrayOfRESTDataSource&gt; </code></pre> <p><code>RESTDataSource</code> can occur 0-n times.</p> <p>And here's my classes:</p> <pre><code>[DataContract( Namespace = "http://SwitchKing.Common/Entities/RESTSimplified/2010/07" )] public class ArrayOfRESTDataSource { public RESTDataSource[] Data { set; get; } } [DataContract( Namespace = "http://SwitchKing.Common/Entities/RESTSimplified/2010/07" )] public class RESTDataSource { [DataMember] public bool Enabled { get; set; } [DataMember] public string Description { get; set; } } </code></pre> <p>I read the above XML data from a server like this:</p> <pre><code>WebRequest client = WebRequest.Create( "http://server:80/datasources" ); using( StreamReader sr = new StreamReader( client.GetResponse().GetResponseStream()) ) { string xml = sr.ReadToEnd(); var response = ServiceStack.Text.XmlSerializer.DeserializeFromString&lt;ArrayOfRESTDataSource&gt;( xml ); } </code></pre> <p>My question is: What do I need to change or decorate <code>public RESTDataSource[] Data</code> with to get the deseralization to work for the array? Serializing single RESTDataSource items work just fine, its just the array I can't get to work.</p> <p>Thanks in advance.</p> <p><em>Update 1</em></p> <p>As @mythz suggested, I updated my code to this, but response.Data is still null. What did I not understand?</p> <pre><code>[DataContract( Namespace = "http://SwitchKing.Common/Entities/RESTSimplified/2010/07" )] public class ArrayOfRESTDataSource { [DataMember] public DataSource Data { set; get; } } [CollectionDataContract( ItemName = "RESTDataSource" )] public class DataSource : List&lt;RESTDataSource&gt; { public DataSource() { } public DataSource( IEnumerable&lt;RESTDataSource&gt; collection ) : base( collection ) { } } </code></pre> <p><em>Update 2</em></p> <p>The solution is in @mythz answer below, but just for completeness/clarity: What I did wrong was to add another level in my DTOs - the top-level class <code>ArrayOfRESTDataSource</code> is the one that actually has the sub items in XML so it is that one that should be of a collection type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload