2016-02-26 77 views
0

我想反序列化xml格式的HTTPresponse到一個對象。反序列化xml時面臨的問題Http響應

XML響應:

<?xml version="1.0" encoding="utf-8" ?> 
- <NPIList> 
- <NPI> 
    <NPI>1003000118</NPI> 
    <EntityType>Organization</EntityType> 
    <IsOrgSubpart>N</IsOrgSubpart> 
    <OrgName>STEVEN ENGEL PEDIATRICS</OrgName> 
    <FirstLineMailingAddress>1700 NEUSE BLVD</FirstLineMailingAddress> 
    <MailingAddressCityName>NEW BERN</MailingAddressCityName> 
    <MailingAddressStateName>NC</MailingAddressStateName> 
    <MailingAddressPostalCode>28560-2304</MailingAddressPostalCode> 
    <MailingAddressCountryCode>US</MailingAddressCountryCode> 
    <MailingAddressTelephoneNumber>252-637-3799</MailingAddressTelephoneNumber> 
    <MailingAddressFaxNumber>252-633-0944</MailingAddressFaxNumber> 
    <FirstLinePracticeLocationAddress>1700 NEUSE BLVD</FirstLinePracticeLocationAddress> 
    <PracticeLocationAddressCityName>NEW BERN</PracticeLocationAddressCityName> 
    </NPI> 
    </NPIList> 

我的代碼檢索XMLHTTP響應和反序列化如下:

using (var response = (HttpWebResponse)request.GetResponse()) 
     { 
     var responseValue = string.Empty; 

     if (response.StatusCode != HttpStatusCode.OK) 
     { 
      var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode); 
      throw new ApplicationException(message); 
     } 
     Stream respStream = response.GetResponseStream(); 
     XmlSerializer ser = new XmlSerializer(typeof(NPIList)); 
     var resp = (NPIList)ser.Deserialize(respStream); 

        response.Close(); 
} 

我已經創建了我的對象類

public class NPIList 
    { 
     public List<NPIObj> NPI { get; set; } 
    } 

我收到對變量「resp」計爲零。

這裏是我NPIObj類:

public class NPIObj 
    { 
     public string EntityType { get; set; } 
     public string FirstLineMailingAddress { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string MailingAddressCityName { get; set; } 
     public string MailingAddressCountryCode { get; set; } 
     public string MailingAddressPostalCode { get; set; } 
     public string MailingAddressStateName { get; set; } 
     public string MiddleName { get; set; } 
     public string NamePrefix { get; set; } 
     public string NPI { get; set; } 
     public string OrgName { get; set; } 
     public string SecondLineMailingAddress { get; set; } 
    } 

誰能幫我可能是什麼問題。

回答

0

嘗試添加XmlElement屬性,如:

public class NPIList 
    { 
     [XmlElement("NPI")] 
     public List<NPIObj> NPI { get; set; } 
    } 
+0

感謝bkdev.I試圖與代碼段和它的工作的罰款。但是這些值不會複製到NPIObj類的屬性中。我編輯並添加了NPIOBJ類。 –

+0

感謝您分享NPIOBJ類。那麼你可以簡單地使用XmlElement屬性。請參閱上面的編輯 – bkdev

+0

感謝它的工作.. –