2

I have to deserialize the following XML:

<section>
 <class_id>Cls101</class_id>
 <sect_no>1</sect_no>
 <class_section>class section</class_section>
 <meet_days>
   <day>M</day>
   <day>T</day>
   <day>W</day>
 </meet_days>
</section>

For which I have created the classes as below:

[Serializable]
[XmlRoot("section")]
public class Section
{
     [XmlElement("class_id")]
     public string ClassId { get; set; }

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

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

     [XmlArray("meet_days")]
     [XmlArrayItem("day")]
     public List<Days> MeetDays { get; set; }
}

[Serializable]
public class Days
{
    [XmlElement("day")]
    public string Day { get; set; }
}

In the deserialized object I am getting Day as null for all 3 rows.

What am I missing here?

0

2 Answers 2

1

Because Day only has one property which is a string, consider removing the Day class and changing the Section class to read:

[XmlArray("meet_days")]
[XmlArrayItem("day")]
public List<string> MeetDays { get; set; }

That way, the M, T and W values get mapped to strings rather than a string within the Day class.

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

1 Comment

Thanks Steve. I tried that earlier and it works. But I don't need string. I need the value to be mapped to Day property.
1

You are not using XmlArrayItem correctly. It designates the types allowed as items in the deserialization.

Try:

[XmlArrayItem(typeof(Days))]

Comments

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.