2012-03-26 166 views
2

我不明白爲什麼對象爲null:反序列化XML的複雜對象

 WebClient browse = new WebClient(); 
     StreamReader res = new StreamReader(browse.OpenRead("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe")); 
     string result = res.ReadToEnd(); 

     XmlDocument xmltrackinfo = new XmlDocument(); 
     xmltrackinfo.InnerXml = result; 



     XmlRootAttribute xRoot = new XmlRootAttribute(); 
     xRoot.ElementName = "lfm"; 
     xRoot.IsNullable = true; 

     XmlSerializer xs = new XmlSerializer(typeof(fm), xRoot); 

     fm rez = (fm) xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result))); 

對象模型:

[Serializable()] 
[XmlRoot(ElementName = "lfm", IsNullable = true)] 
public class fm 
{ 
    [XmlElement("lfm")] 
    public Track lfm { get; set; } 
} 

[Serializable()] 
[XmlRoot(ElementName = "artist", IsNullable = true)] 
public class artist 
{ 
    public string name { get; set; } 
    public string mbid { get; set; } 
    public string url { get; set; } 
} 

[Serializable()] 
[XmlRoot(ElementName = "album", IsNullable = true)] 
public class album 
{ 
    public string artist { get; set; } 
    public string title { get; set; } 
    public string mbid { get; set; } 
    public string url { get; set; } 
    public List<string> image { get; set; } 
} 

[Serializable()] 
[XmlRoot(ElementName = "tag", IsNullable = true)] 
public class tag 
{ 
    public string name { get; set; } 
    public string url { get; set; } 

} 

[Serializable()] 
[XmlRoot(ElementName = "wiki", IsNullable = true)] 
public class wiki 
{ 
    public string summary { get; set; } 
    public string content { get; set; } 
} 

[Serializable()] 
[XmlRoot(ElementName = "track", IsNullable = true)] 
public class Track 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public string mbid { get; set; } 
    public string url { get; set; } 
    public string duration { get; set; } 
    public string streamable { get; set; } 
    public string listeners { get; set; } 
    public string playcount { get; set; } 
    public artist artist { get; set; } 
    public album album { get; set; } 
    public List<tag> toptags { get; set; } 
    public wiki wiki { get; set; } 

} 

和XML:

http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe

那麼應該怎麼我做?

回答

2

嘗試將您的fm類重命名爲lfm

public class lfm 
{ 
    public Track track { get; set; } 
} 

,然後你也可以擺脫xRoot變量:

XmlSerializer xs = new XmlSerializer(typeof(lfm)); 
lfm rez = (lfm) xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result))); 

而且你不需要[Serializable]屬性。這用於二進制序列化,並且被XmlSerializer類完全忽略。

+0

是的,它的作品!另外我刪除了所有屬性 – Evgeniy 2012-03-26 11:14:32

1

fm類的lfm屬性必須有軌道作爲的XmlElement:

[Serializable()] 
[XmlRoot(ElementName = "lfm", IsNullable = true)] 
public class fm 
{ 
    [XmlElement("track")] 
    public Track lfm { get; set; } 
} 
+0

是的,這就是要點 – Evgeniy 2012-03-26 14:00:43