2013-11-22 75 views
1

我收到以下錯誤,試圖反序列化xml。這會產生錯誤:XMLSerializer不反序列化XML

XmlSerializer serializer = new XmlSerializer(typeof(PrivateOptionsAPIResponse));
var result = serializer.Deserialize(streamReader);

例外:

System.InvalidOperationException了抓
消息=有XML文檔中出現錯誤(0,0)
的InnerException信息:System.Xml.XmlException
消息=缺少根元素
Source = System.Xml

我不知道如何解決該問題。請求返回下面的XML:

<PrivateOptionsAPIResponse> <CountiesForPostalCodeResponse> <Counties> <County> <CountyName>PRINCE WILLIAM</CountyName> <StateCode>VA</StateCode> </County> <County> <CountyName>MANASSAS CITY</CountyName> <StateCode>VA</StateCode> </County> <County> <CountyName>MANASSAS PARK CITY</CountyName> <StateCode>VA</StateCode> </County> </Counties> </CountiesForPostalCodeResponse> </PrivateOptionsAPIResponse>

我用XSD.EXE生成的類。 (由xsd.exe工具生成)上PrivateOptionsAPIResponse的定義所示:

公共部分類PrivateOptionsAPIResponse {

private object itemField; 

/// <remarks/> 
[System.Xml.Serialization.XmlElementAttribute("CountiesForPostalCodeResponse", typeof(ZipCodeValidationResponse))] 
[System.Xml.Serialization.XmlElementAttribute("PlanDetailsForIndividualOrFamilyResponse", typeof(IndividualPlanBenefitResponse))] 
[System.Xml.Serialization.XmlElementAttribute("PlansForIndividualOrFamilyResponse", typeof(IndividualPlanQuoteResponse))] 
[System.Xml.Serialization.XmlElementAttribute("ProductDetailsForSmallGroupResponse", typeof(SmallGroupProductBenefitResponse))] 
[System.Xml.Serialization.XmlElementAttribute("ProductsForSmallGroupResponse", typeof(SmallGroupProductQuoteResponse))] 
public object Item { 
    get { 
     return this.itemField; 
    } 
    set { 
     this.itemField = value; 
    } 
} 

}

如果我然後瀏覽ZipCodeValidationResponse定義它示出了這一點:

public partial class ZipCodeValidationResponse { 

private CountyType[] countiesField; 

/// <remarks/> 
[System.Xml.Serialization.XmlArrayItemAttribute("County", IsNullable=false)] 
public CountyType[] Counties { 
    get { 
     return this.countiesField; 
    } 
    set { 
     this.countiesField = value; 
    } 
} 

}

如果我然後瀏覽上CountyType定義我看到這一點:

public partial class CountyType { 

private string countyNameField; 

private StateAbbreviationType stateCodeField; 

/// <remarks/> 
public string CountyName { 
    get { 
     return this.countyNameField; 
    } 
    set { 
     this.countyNameField = value; 
    } 
} 

/// <remarks/> 
public StateAbbreviationType StateCode { 
    get { 
     return this.stateCodeField; 
    } 
    set { 
     this.stateCodeField = value; 
    } 
} 

}

----------工作液----------------:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
     { 
      string status = ((HttpWebResponse)response).StatusDescription; 

      if(status == "OK") 
      { 
       using (Stream responseStream = response.GetResponseStream()) 
       { 
        using (StreamReader reader = new StreamReader(responseStream)) 
        { 
         var xmlSerializer = new XmlSerializer(typeof(PrivateOptionsAPIResponse)); 
         var privateOptionsAPIResponse = xmlSerializer.Deserialize(reader) as PrivateOptionsAPIResponse; 
        } 
       }      
      } 
     } 
+1

假設閱讀器的內容已經過驗證,您可能必須用「streamReader.MoveToContent()」強制閱讀器進入內容位置。 –

回答

1

你如何聲明你的streamReader?看看它的內容,你很可能會看到它是空的或不包含完整的XML文檔。

+0

聲明這樣的streamRader:using(var streamReader = new StreamReader(response.GetResponseStream())) – obautista

+0

你的評論讓我回顧我是如何聲明的。我已經用工作解決方案更新了我的原始帖子。謝謝。 – obautista

0

首先,檢查您的XML是否有效。從給定的例外情況看來,您似乎提供了無效的xml文檔。非常感謝,如果你可以發佈你試圖序列化的內容(XML)。

+0

在StreamReader上運行:var xdoc = XDocument.Parse(streamReader.ReadToEnd());返回原始帖子中的字符串(XML)。 – obautista