RajenK
RajenK

Reputation: 1423

XmlSerializer Deserialize returns empty array

I'm trying to deserialize the following XML (excerpt):

<NSArray> 
  <Song id="23507" type="Song"> 
    <title>Waking The Demon</title> 
    <artist id="17" type="Artist"> 
      <nameWithoutThePrefix>Bullet For My Valentine</nameWithoutThePrefix> 
      <useThePrefix>false</useThePrefix> 
      <name>Bullet For My Valentine</name> 
    </artist> 
  </Song> 
  <Song id="3663" type="Song"> 
    <title>Hand Of Blood</title> 
    <artist id="17" type="Artist"/> 
  </Song> 
  <Song id="59226" type="Song"> 
    <title>Your Betrayal</title> 
    <artist id="17" type="Artist"/> 
  </Song> 
</NSArray> 

with the following classes:

[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
public class SearchResult
{
    [XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
    public Song[] Items { get; set; }
}

[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Song
{
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public string Title { get; set; }

    [XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
    public Artist Artist { get; set; }

    [XmlAttribute]
    public string Type { get; set; }

    [XmlAttribute]
    public string Id { get; set; }
}

[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
public class Artist
{
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public string NameWithoutThePrefix { get; set; }

    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public string UseThePrefix { get; set; }

    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public string Name { get; set; }

    [XmlAttribute]
    public string Type { get; set; }

    [XmlAttribute]
    public string Id { get; set; }
}

and the following code:

    var request = WebRequest.Create(string.Format("http://myurl.com");
    request.BeginGetResponse(GetEventResponseCallback, request);

    private void GetEventResponseCallback(IAsyncResult result)
    {
        var request = (HttpWebRequest)result.AsyncState;
        var response = request.EndGetResponse(result);

        if (response.GetResponseStream() == null) return;
        using (var stream = response.GetResponseStream())
        {
            _xmlReader = XmlReader.Create(stream);
            var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
        }
    }

However, on var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;, the Deserialization executes successfully, but the songs variable does not contain any data. If I inspect with the debugger, it returns Could not evaluate expression for all the values in the array.

Any hints? Thanks.

Upvotes: 2

Views: 6304

Answers (2)

pmartin
pmartin

Reputation: 2741

Your SearchResult class needs some fixing. You're really close, the code is only missing a few element names and the serializable attributes.

Here's a class that works:

[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
[Serializable]
public class SearchResult
{
    [XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
    public Song[] Items { get; set; }
}

[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName="Song", Namespace = "", IsNullable = false)]
[Serializable]
public class Song
{
    [XmlElement("title", Form = XmlSchemaForm.Unqualified)]
    public string Title { get; set; }

    [XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
    public Artist Artist { get; set; }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("id")]
    public string Id { get; set; }
}

[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[Serializable]
public class Artist
{
    [XmlElement("nameWithoutThePrefix", Form = XmlSchemaForm.Unqualified)]
    public string NameWithoutThePrefix { get; set; }

    [XmlElement("useThePrefix", Form = XmlSchemaForm.Unqualified)]
    public string UseThePrefix { get; set; }

    [XmlElement("name", Form = XmlSchemaForm.Unqualified)]
    public string Name { get; set; }

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("id")]
    public string Id { get; set; }
}

Upvotes: 4

Jean Libera
Jean Libera

Reputation: 629

I have a class that I am serializing and deserializing. Compared to the attributes you are using... you have a lot that I don't have (on the class), but one that you are missing is [Serializable]

Mine looks like this:

[Serializable]
public class Settings
{
    [XmlElement]
    public int Version { get; set; }

    [XmlElement]
    public string Name { get; set; }

    // more settings
}

So start with something simple like this, that works, then add in each new tag one at a time, and you will quickly find which one is causing it to break.

Since you are having trouble deserializing, here is the code I am using. In another class, I have:

public string FilePath { get; set; }
public Settings LoadSettings()
{
    XmlSerializer serializer = new XmlSerializer(typeof(Settings));

    Settings settings = null;

    using(TextReader reader = new StreamReader(this.FilePath))
    {
        settings = (Settings)serializer.Deserialize(reader);
    }

    return settings;
}

Upvotes: 0

Related Questions