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

Hi,

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">
     </Addresses>
</Employee>

So class structure i would follow

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

        [XmlIgnore()]
        public string Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
            }
        }
}


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

end class


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

            [XmlIgnore()]
            public int AddressId
            {
                get
                {
                    return _addressId;
                }
                set
                {
                    _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.
Thanks,
Rithesh

Star79Asked:
Who is Participating?

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

x
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

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayattribute.aspx
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", 
   IsNullable=true,
   Type = typeof(Item),
   Namespace = "http://www.cpandl.com"),
   XmlArrayItem(ElementName = "BookItem", 
   IsNullable = true, 
   Type = typeof(BookItem),
   Namespace = "http://www.cohowinery.com")]
   [XmlArray]
   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.