簡體   English   中英

XmlSerializer反序列化返回空數組

[英]XmlSerializer Deserialize returns empty array

我正在嘗試反序列化以下XML(摘錄):

<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> 

具有以下類別:

[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; }
}

和以下代碼:

    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;
        }
    }

但是,在var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult; ,反序列化將成功執行,但是songs變量不包含任何數據。 如果我使用調試器進行檢查,它將返回Could not evaluate expression為數組中的所有值Could not evaluate expression

有什么提示嗎? 謝謝。

您的SearchResult類需要一些修復。 您真的很接近,代碼僅缺少一些元素名稱和可序列化的屬性。

這是一個有效的課程:

[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; }
}

我有一個要序列化和反序列化的類。 與您使用的屬性相比...(在課堂上)我沒有很多,但是[Serializable]是您所缺少的

我的看起來像這樣:

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

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

    // more settings
}

因此,從類似這樣的簡單操作開始,然后一次添加一個新標簽,您會很快發現哪個標簽造成了損壞。

由於您在反序列化方面遇到麻煩,因此這是我正在使用的代碼。 在另一堂課中,我有:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM