2016-02-26 45 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

感谢它的工作.. –