Xml DeSerialization - Array Elements

lookin my XML example, how I do to Deserialize it? But I need to Deserialize the Xml Element array. How I do it?

<hotels num="1">
<hotel num="1" item="2"></hotel>
<hotel num="2" item="2"></hotel>
<hotel num="3" item="2"></hotel>
<hotel num="4" item="2"></hotel>
<hotel num="5" item="2"></hotel>
</hotels>

[Serializable]
[XmlRoot("hotels")]
public class Hotels
{
[XmlElement("id")]
public string id {  get; set; }
[XmlElement("hotel")]
public Hotel hotel { get; set; }
}

[Serializable]
[XmlRoot("hotel")]
public class Hotel
{
[XmlElement("id")]
public string id {  get; set; }
[XmlElement("item")]
public string item {  get; set; }
}

Answers


Try like this:

[XmlRoot("hotels")]
public class HotelData
{
    [XmlAttribute("num")]
    public string Id { get; set; }

    [XmlElement("hotel")]
    public List<Hotel> Hotels { get; set; }
}

public class Hotel
{
    [XmlAttribute("num")]
    public string Id { get; set; }

    [XmlAttribute("item")]
    public string Item { get; set; }
}

and then deserialize:

public class Program
{
    static void Main()
    {
        var serializer = new XmlSerializer(typeof(HotelData));
        using (var reader = XmlReader.Create("test.xml"))
        {
            var data = (HotelData)serializer.Deserialize(reader);
            Console.WriteLine(data.Id);
            foreach (var hotel in data.Hotels)
            {
                Console.WriteLine("num: {0}, item:{1}", hotel.Id, hotel.Item);
            }
        }
    }
}

Need Your Help

ReferenceError: before is not defined (mocha/protractor)

reactjs protractor mocha e2e-testing

I am using protractor with mocha in react application. When trying to use before() or after() functions it gives me an error:

Perl array overriding

arrays perl loops overriding

I am trying to do sum of the array in my perl code but I was not able to get the right output.