'ArrayList' is an invalid value for the XmlElementAttribute.DataType property. The property may only be specified for primitive types.


I am stuck with a web service problem.
I need my Web service to return an xml in the following format

<?xml version="1.0" encoding="utf-8" ?>
<Employee Type="HR">
     <Addresses AddressId="1">

So class structure i would follow

XmlRoot(ElementName = "Employee"), Serializable]
public class Employee
[XmlAttribute(AttributeName = "Type", DataType = "string", Form = XmlSchemaForm.Qualified, Namespace = "")]
        public string _Type;

        public string Type
                return _Type;
                _Type = value;

 [XmlElement(ElementName = "addresses", IsNullable = false, Form = XmlSchemaForm.Qualified, DataType = "Arraylist")]
        public Arraylist _addresses;

end class

[XmlRoot(ElementName = "Addresses"), Serializable]
public class Addresses
  [XmlAttribute(AttributeName = "AddressId", DataType = "int", Form = XmlSchemaForm.Qualified, Namespace = "")]
            public int _addressId;

            public int AddressId
                    return _addressId;
                    _addressId = value;


I am trying to serilaize the above using the collections but i am getting the following error
'ArrayList' is an invalid value for the XmlElementAttribute.DataType property. The property may only be specified for primitive types.

any help will be highly appreciated.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I would assume that you need to fully qualify the name, and get the case correct:  System.Collections.ArrayList, not "Arraylist".
Star79Author Commented:
Sorry i tried, it doesnt work. Any other option?
Bob LearnedCommented:
I don't use XML serialization very much anymore, so I am required to think about this more closely.

I believe that you are looking for the XmlArrayAttribute

public class MyRootClass
   private Item [] items;

   /* Here is a simple way to serialize the array as XML. Using the
      XmlArrayAttribute, assign an element name and namespace. The
      IsNullable property determines whether the element will be 
      generated if the field is set to a null value. If set to true,
      the default, setting it to a null value will cause the XML
      xsi:null attribute to be generated. */
   [XmlArray(ElementName = "MyStrings",
   Namespace = "http://www.cpandl.com", IsNullable = true)]
   public string[] MyStringArray;

   /* Here is a more complex example of applying an 
      XmlArrayAttribute. The Items property can contain both Item 
      and BookItem objects. Use the XmlArrayItemAttribute to specify
      that both types can be inserted into the array. */
   [XmlArrayItem(ElementName= "Item", 
   Type = typeof(Item),
   Namespace = "http://www.cpandl.com"),
   XmlArrayItem(ElementName = "BookItem", 
   IsNullable = true, 
   Type = typeof(BookItem),
   Namespace = "http://www.cohowinery.com")]
   public Item []Items
      get{return items;}
      set{items = value;}

Open in new window

Miguel OzSoftware EngineerCommented:
ArrayList is not serializable.
Use the generic list class:
List<T> where T is you class name. e.g List <Employee>

no need to use arrays. ArrayList has been superseeded by List<T>.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.