1

I've this element in my class

[XmlArray("photos")]         
public List<zPhoto> EntityPhotos;

when I serialize my class I get this :

<photos>
    <zPhoto id="73102" type="a" />
    <zPhoto id="73102" type="b"/>
    <zPhoto id="73105" type="a" />
    <zPhoto id="73105" type="b" />
</photos>

In order to simplify xpath query I want to add a new directive that will give me a result like

<photos count="2" >
    <zPhoto id="73102" type="a" />
    <zPhoto id="73102" type="b"/>
    <zPhoto id="73105" type="a" />
    <zPhoto id="73105" type="b" />
</photos>

I want to add trival attributes name and set the value. is it possible?

Christophe Debove
  • 6,088
  • 20
  • 73
  • 124

3 Answers3

2

Add the attribute above your member variable

 [XmlAttribute("count")]

EDIT

This has been covered in a previous question on SO

How do I add a attribute to a XmlArray element (XML Serialization)?

Community
  • 1
  • 1
nik0lai
  • 2,585
  • 23
  • 37
0

One way would be to introduce a custom generic collection class that serializes itself the way you want.

bitbonk
  • 48,890
  • 37
  • 186
  • 278
-2

You can replace String type with you ZPhotos type.

[XmlRoot("EntityPhotos")]
    public class EntityPhotos
    {
        private List<String> _photos;

        public EntityPhotos()
        {
            _photos = new List<string>
            {
                "One.jpg",
                "Two.png",
                "Three.gif"
            };
        }

        [XmlElement("Photos")]
        public String[] Photos
        {
            get
            {
                return _photos.ToArray();
            }

            set  {;}

        }

        [XmlAttribute("Count")]
        public Int32 Count
        {
            get
            {
                if (Photos != null)
                    return Photos.Length;
                else
                    return 0;
            }

            set{;}
        }
    }
Vijay Sirigiri
  • 4,653
  • 29
  • 31