Deserializing Xml string into object with List<>

Carlos Landeras

I'm trying to deserialize a xml string to a custom class, but I can get my "Riesgo" field filled with asegurado class:

<xml xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
    <CodPostal>28029</CodPostal>
    <Canal>216 </Canal>
    <FormaPago>M</FormaPago>
    <ConSeguro>N</ConSeguro>
    <FechaEfecto>01/01/2014</FechaEfecto>
    <Riesgo>
        <asegurado>
            <sexo>H</sexo>
            <edad>37</edad>
            <parentesco>M</parentesco>
        </asegurado>
        <asegurado>
            <sexo>M</sexo>
            <edad>34</edad>
            <parentesco>C</parentesco>
        </asegurado>
        <asegurado>
            <sexo>H</sexo>
            <edad>4</edad>
            <parentesco>D</parentesco>
        </asegurado>
    </Riesgo>
</xml>

I have tried several things but the List inside Riesgo always come null.

 public class TarificadorObject
    {

        [DataContract]
        [Serializable]
        [XmlRoot("xml")]
        public class TarificadorIn
        {
            [XmlElement("CodPostal")]
            public Int32 CodPostal { get; set; }
            [XmlElement("Canal")]
            public Int32 Canal { get; set; }

            [XmlElement("Riesgo")]
            [XmlArrayItem("asegurado", Type = typeof (Asegurado))]
            public List<Asegurado> asegurado
            {
                get { return _asegurados;  }
                set { _asegurados = value; }
            }

            [XmlElement("FechaEfecto")]
            public string FechaEfecto { get; set; }

            private List<Asegurado> _asegurados = new List<Asegurado>();
        }



        [Serializable]
        public class Asegurado
        {
            [XmlAttribute("sexo")]
            public string sexo { get; set; }
            [XmlAttribute("edad")]
            public Int32 edad { get; set; }
            [XmlAttribute("parentesco")]
            public string parentesco { get; set; }
        }
    }
Marc Gravell

You want:

[XmlArray("Riesgo")]
[XmlArrayItem("asegurado", Type = typeof (Asegurado))]

not XmlElementAttribute (which causes the list contents to be placed directly under the parent).

Actually, you can be more frugal if you want; the Type is implied (from the list) and can be omitted if you want.

Note also that these are wrong:

[XmlAttribute("sexo")]
public string sexo { get; set; }
[XmlAttribute("edad")]
public Int32 edad { get; set; }
[XmlAttribute("parentesco")]
public string parentesco { get; set; }

They are not xml attributes - they are elements; you can replace that with:

public string sexo { get; set; }
public int edad { get; set; }
public string parentesco { get; set; }

(the default behavior is an element named for the property)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Deserializing string using ServiceStack

분류에서Dev

List add a ref to string object

분류에서Dev

List <Tuple <string, List <Object >>> 병합

분류에서Dev

Deserializing weirdly formatted JSON to an object in C#

분류에서Dev

Java SerialIzation: 'ClassNotFoundException' when deserializing an Object

분류에서Dev

List <Map <String, Object >> to List <Map <String, Object >> group by key with nulls

분류에서Dev

List <IDictionary <string, object >> 추가 추가 List <IDictionary <string, object >> with title

분류에서Dev

Dictionary <string, object> 대 List <string> + BinarySearch의 성능

분류에서Dev

XML을 List <Object>로 deserialize하는 방법

분류에서Dev

JSon.NET deserializing object doesn't work

분류에서Dev

List <List <object >>를 List <List <string >>으로 변환

분류에서Dev

How to convert xml string to an object using c#

분류에서Dev

DataContractJsonSerializer deserializing List <T> 오류 발생

분류에서Dev

Convert string with XML to datatable, list or something more manageable?

분류에서Dev

Map <String, Object>를 Map <String, List <Object >>로 변환

분류에서Dev

C # LINQ 쿼리 a List <Dicitonary <string, object >>

분류에서Dev

ItemsSource가 List <Dictionary <string, object >> 인 ListBox

분류에서Dev

List <object> 유형의 개체를 List <string>으로 변환

분류에서Dev

c #-List <string>의 항목을 List <object>에 넣는 방법

분류에서Dev

Generics :`List <String>`을`List <Object>`에 추가합니다.

분류에서Dev

List <List <Object >> to String [] []. 방법이 있습니까?

분류에서Dev

List <string> 및 List <Object>를 사전에 삽입

분류에서Dev

Java Small Map <String, List> vs Large Map <Object, String> 성능

분류에서Dev

foreach list <object> in object

분류에서Dev

LINQ를 사용하여 Dictionary <int, List <Tuple <string, object >>>에서 Dictionary <int, List <Tuple <string, object, AnEnum >>>으로

분류에서Dev

XML 스키마를 사용하여 List <String>을 XML로 구문 분석

분류에서Dev

XML 직렬화 및 역 직렬화 List <string> in C #

분류에서Dev

Dictionary <string, List <Custom Type >>을 XML로 직렬화하는 방법

분류에서Dev

xml 속성을 List <String>으로 역 직렬화

Related 관련 기사

  1. 1

    Deserializing string using ServiceStack

  2. 2

    List add a ref to string object

  3. 3

    List <Tuple <string, List <Object >>> 병합

  4. 4

    Deserializing weirdly formatted JSON to an object in C#

  5. 5

    Java SerialIzation: 'ClassNotFoundException' when deserializing an Object

  6. 6

    List <Map <String, Object >> to List <Map <String, Object >> group by key with nulls

  7. 7

    List <IDictionary <string, object >> 추가 추가 List <IDictionary <string, object >> with title

  8. 8

    Dictionary <string, object> 대 List <string> + BinarySearch의 성능

  9. 9

    XML을 List <Object>로 deserialize하는 방법

  10. 10

    JSon.NET deserializing object doesn't work

  11. 11

    List <List <object >>를 List <List <string >>으로 변환

  12. 12

    How to convert xml string to an object using c#

  13. 13

    DataContractJsonSerializer deserializing List <T> 오류 발생

  14. 14

    Convert string with XML to datatable, list or something more manageable?

  15. 15

    Map <String, Object>를 Map <String, List <Object >>로 변환

  16. 16

    C # LINQ 쿼리 a List <Dicitonary <string, object >>

  17. 17

    ItemsSource가 List <Dictionary <string, object >> 인 ListBox

  18. 18

    List <object> 유형의 개체를 List <string>으로 변환

  19. 19

    c #-List <string>의 항목을 List <object>에 넣는 방법

  20. 20

    Generics :`List <String>`을`List <Object>`에 추가합니다.

  21. 21

    List <List <Object >> to String [] []. 방법이 있습니까?

  22. 22

    List <string> 및 List <Object>를 사전에 삽입

  23. 23

    Java Small Map <String, List> vs Large Map <Object, String> 성능

  24. 24

    foreach list <object> in object

  25. 25

    LINQ를 사용하여 Dictionary <int, List <Tuple <string, object >>>에서 Dictionary <int, List <Tuple <string, object, AnEnum >>>으로

  26. 26

    XML 스키마를 사용하여 List <String>을 XML로 구문 분석

  27. 27

    XML 직렬화 및 역 직렬화 List <string> in C #

  28. 28

    Dictionary <string, List <Custom Type >>을 XML로 직렬화하는 방법

  29. 29

    xml 속성을 List <String>으로 역 직렬화

뜨겁다태그

보관