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?

3 Answers 3

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)?

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

3 Comments

Have you made the variable public?
If you still struggle check this page: stackoverflow.com/questions/1052556/…
Ok it's good can you edit your answer to add the url Thanks you
0

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

Comments

-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{;}
        }
    }

3 Comments

I don't understand why you tell me to replace with a string, In my answer I take photo but it could be other thing.
Instead of using String[] Photos, you could replace it with ZPhoto[] Photos as you wanted ZPhoto to serialize.
OK I understand can you edit your answer cause I can't dismiss my vote down

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.