1

I have been trying to solve this my self but not finding out.

I have an XML which includes array of responseElementList

<responseElementList name="something">
<valueList name="NAT_TEXT">XXX</valueList>
<value name="XXX_MAIN">NONE</value>
<value name="XXX_SEC">NONE</value>
<value name="XXX_XXX">NO</value>
<value name="XXX">YES</value>
<value name="XXX_NET">NO</value>
</responseElementList>

and I am trying to deserialize it with

    public class responseElementList
    {
        [XmlAttribute("name")]
        public String name { get; set; }

        [XmlElement("valueList")]
        public ValueModel valueList { get; set; }

        [XmlElement("value")]
        public List<ValueModel> value { get; set; }
    }

    public class ValueModel
    {
        [XmlAttribute("name")]
        public String name { get; set; }

        [XmlText]
        public String Value { get; set; }
    }

I am always getting an empty list with this deserialization. ( valueList populates just fine )

0

1 Answer 1

1

Not seeing your de-serialization, so don't know what's wrong, but the following works:

public class responseElementList
{
    [XmlAttribute("name")]
    public String name { get; set; }

    [XmlElement("valueList")]
    public ValueModel valueList { get; set; }

    [XmlElement("value")]
    public List<ValueModel> value { get; set; }
}

public class ValueModel
{
    [XmlAttribute("name")]
    public String name { get; set; }

    [XmlText]
    public String Value { get; set; }
}

public static T DeSerialize<T>(string xml)
{
    T result = default(T);
    using (TextReader reader = new StringReader(xml))
    {
        var ser = new XmlSerializer(typeof(T));
        result = (T)ser.Deserialize(reader);
    }
    return result;
}

void Main()
{
    var a = @"<responseElementList name='something'>
<valueList name='NAT_TEXT'>XXX</valueList>
<value name='XXX_MAIN'>NONE</value>
<value name='XXX_SEC'>NONE</value>
<value name='XXX_XXX'>NO</value>
<value name='XXX'>YES</value>
<value name='XXX_NET'>NO</value>
</responseElementList>";

    var obj = DataHelper.DeSerialize<responseElementList>(a);
    obj.Dump();
}

gives

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

great thanks, this was the deserializer implementation of ReSharper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.