1

I have the following XML:

<Product>
    <Categories>
        <Id>1</Id>
        <Id>2</Id>
        <Id>3</Id>
        <Id>4</Id>
        <Id>5</Id>
        <Id>6</Id>
        <Id>7</Id>
        <Id>8</Id>
    </Categories>
</Product>

I'm deserializing it to the .NET type using XmlSerializer. My goal is to have it deserealized as:

public class Product
{
    public List<int> Categories { get; set; }
}

Unfortunately I didn't manage to deserealize it to such type (the Categories property is empty). Any ideas how can I do it?

I managed to do it with, but I'm not happy with such solution.

[Serializable]
public class Categories
{
    [XmlElement]
    public List<Int32> Id { get; set; }
}

public class Product
{
    public Categories Categories { get; set; }
}

I know, I can do it with Linq to XML, but I wonder if I can do it with XmlSerializer.

1 Answer 1

4

You can use the XmlArray and XmlArrayItem attributes:

public class Product
{
    [XmlArray]
    [XmlArrayItem("Id")]
    public List<int> Categories { get; set; }
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.