2012-05-03 2 views
0

REST API에서 응답을 deserialize하려고합니다.C# XML 배열 요소를 deserialize null

"<FieldListDTO xmlns=\"api.playcento.com/1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"> 
<Allfield> 
<FieldDTO> 
<Fieldname>Mobile nr</Fieldname> 
<Fieldtype>T</Fieldtype> 
<Fieldvalue>003241234578</Fieldvalue> 
<Fk_id_page>CP584ea74ce5ad4e2d8561d75fc6944f96</Fk_id_page> 
<Id_field>FI152dcde5ef9849898b12d6a3f2cdb4ee</Id_field> 
<Required>true</Required> 
</FieldDTO> 
</Allfield> 
<Totalcount>1</Totalcount> 
</FieldListDTO>" 

필드 클래스 : API를 호출하고 직렬화 복원

namespace PlaycentoAPI.Model 
{ 
    [XmlRoot("FieldListDTO",Namespace = "api.playcento.com/1.0")] 
    [XmlType("FieldListDTO")] 
    public class FieldListDTO 
    { 
     public FieldListDTO() { } 

     [XmlElement("Totalcount")] 
     public int TotalCount { get; set; } 

     [XmlArray("Allfield")] 
     [XmlArrayItem("FieldDTO", typeof(Field))] 
     public Field[] Field { get; set; } 

    } 
    [XmlRoot("FieldDTO", Namespace = "api.paycento.com/1.0")] 
    [XmlType("FieldDTO")] 
    public class Field 
    { 
     public Field() 
     { 
     } 

     [XmlElement("Id_field")] 
     public string ID_Field { get; set; } 
     [XmlElement("Fieldtype")] 
     public string FieldType { get; set; } 
     [XmlElement("Fk_id_page")] 
     public string FK_ID_PAGE { get; set; } 
     [XmlElement("Required")] 
     public bool Required { get; set; } 
     [XmlElement("Fieldname")] 
     public string FieldName { get; set; } 
     [XmlElement("Fieldvalue")] 
     public string FieldValue { get; set; } 
    } 
} 

내 코드 :

string response = Helper.PerformAndReadHttpRequest(uri, "GET", ""); 
       FieldListDTO myObject; 
       XmlReaderSettings settings = new XmlReaderSettings(); 
       using (StringReader textReader = new StringReader(response)) 
       { 
        using (XmlReader xmlReader = XmlReader.Create(textReader, settings)) 
        { 
         XmlSerializer mySerializer = new XmlSerializer(typeof(FieldListDTO)); 
         myObject = (FieldListDTO)mySerializer.Deserialize(xmlReader); 
        } 
       } 
       return myObject.Field; 

를 내 실제 응답에서, 나는 다시 14 FieldDTO 년대를 얻고있다. xml을 deserialize 한 후에 FieldListDTO myObject는 TotalCount = 14를 포함하고 Field는 14 Field를 포함하는 배열입니다. 그러나이 필드의 모든 속성은 NULL (또는 false)입니다.

다른 API 호출과 동일한 방법을 사용하고 있습니다. 클래스를 비교해 본 결과 클래스 (Field)에 bool 속성이 있다는 점만 다릅니다. 그래서 그게 문제라고 생각했습니다. bool 속성을 문자열로 변경했지만 역 직렬화 후에도 모든 속성이 NULL이었습니다.

답변

1

FieldDTO 클래스의 네임 스페이스가 XML 문서의 네임 스페이스와 일치하지 않습니다.

"api.paycento.com/1.0" 
"api.playcento.com/1.0" 
+0

오 하나님 감사합니다. 저를 찾기 위해 수년이 걸렸을 수 있습니다. – Reinard

관련 문제