2010-09-15 155 views
0

我有一些問題反序列化一個XML文檔。我想反序列化的文件是這樣的:XmlSerializer.Deserialize問題根元素缺失或元素不是預期的

<slt:CreateGiftRegistryResponse xmlns:slt="http://WWW.example.com/"> 
<slt:Response> 
<slt:ResponseCode>ERROR</slt:ResponseCode> 
<slt:ResponseDescription>Request unsuccessfull null</slt:ResponseDescription> 
</slt:Response></slt:CreateGiftRegistryResponse> 

我的類看起來是這樣的:

/// <summary> 
/// response to attempt to add items to a registry 
/// </summary> 
[XmlRoot("CreateGiftRegistryResponse")] 
public class CreateRegistryResponse : ResponseBase 
{ 
    // Constant Declarations 

    // Variable Declarations 

    #region --- Constructors --- 

    public CreateRegistryResponse() 
     : this(String.Empty) { } 

    /// <summary> 
    /// response to attempt to add items to a registry 
    /// </summary> 
    /// <param name="response">xml string</param> 
    public CreateRegistryResponse(string responseXml) 
    { 
     try 
     { 
      Load(responseXml); 
     } 
     catch (Exception ex) 
     { 
      // Report the exception and throw to the caller for handling. 
      ExceptionManager.Publish(ex, 
       "ctor CreateRegistryResponse() failed.", 
       Severity.Fatal); 
      throw; 
     } 
    } 
    #endregion 

    #region --- Properties --- 
    /// <summary> 
    /// structure for the typical response - code and description 
    /// </summary> 
    [XmlElement("Response")] 
    public ResponseWS Response { get; set; } 


    #endregion 

    #region --- Static Methods --- 
    #endregion 

    #region --- CRUD --- 
    #endregion 

    #region --- Validation --- 
    #endregion 

    #region --- Business Methods --- 
    /// <summary> 
    /// Load the web service result string into a Result. 
    /// </summary> 
    public void Load(string response) 
    { 
     try 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(CreateRegistryResponse), this.GetExtraTypes()); 
      byte[] byteArray = Encoding.ASCII.GetBytes(response); 
      MemoryStream stream = new MemoryStream(byteArray); 

      // convert the results into a usable format 
      CreateRegistryResponse formattedResponse = serializer.Deserialize(stream) as CreateRegistryResponse; 

      this.Response = formattedResponse.Response; 
      if (formattedResponse.Response.ResponseCode == ResponseCode.SUCCESS.ToString()) 
      { 
       this.IsSuccessful = true; 
      } 


     } 
     catch (Exception ex) 
     { 
      // Report the exception and throw to the caller for handling. 
      ExceptionManager.Publish(ex, 
       "Load() failed. Unable to authenticate user.", 
       Severity.Fatal); 
      throw; 
     } 
     finally 
     { 
      // 
      // TODO: Add clean-up code here, if needed. 
      // 
     } 
    } 

    /// <summary> 
    /// Get an array of types that are possibly contained within this class 
    /// </summary> 
    public Type[] GetExtraTypes() 
    { 
     try 
     { 
      // 
      // TODO: Add code here. 
      // 
      // build an array of possible types within this type. 
      List<Type> types = new List<Type>(); 
      types.Add(typeof(ResponseWS)); 


      return types.ToArray(); 


     } 
     catch (Exception ex) 
     { 
      // Report the exception and throw to the caller for handling. 
      ExceptionManager.Publish(ex, 
       "GetExtraTypes() failed. Unable to return list", 
       Severity.Fatal); 
      throw; 
     } 
    } 
    #endregion 
} 

當我使用此代碼,我得到這個錯誤:{「HTTP://kiosk.surlatable 「}

如果我將XmlRoot元素更改爲也包含名稱空間,那麼我的錯誤更改爲根元素缺失。

我以爲其中的一個會給我我期望的結果,但事實並非如此。有人可以發現我在這裏失蹤的東西嗎?

回答

3

我試圖讓我的類在反序列化時正確裝飾的方法是使用XSD.exe生成基於XSD的c#類,然後將裝飾與我自己的類進行比較。它不止一次闡明瞭問題。

打開Visual Studio命令提示符,然後:

xsd /c <filename>.xsd 
+0

XSD.exe實際上是罪魁禍首!我基於教程使用它,並且在開始理解時沒有做足夠的閱讀。這個問題肯定與我的類裝飾器。我相信有2個根宣言。它現在工作得很好。 – mkeller 2010-09-18 19:40:40

1

根據XML元素的命名空間設置你的XmlRoot屬性的命名空間屬性。

它是XML文檔的一個片段還是完整的? 我沒有看到XML聲明

+0

謝謝!這與我的問題直接相關。代碼被分成了幾個類,這個層次和這個XML響應的方式看起來讓我頗爲困惑。 – mkeller 2010-09-18 19:41:50

相關問題