Click here to Skip to main content
14,882,311 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am developing WCF application using MVC3.

I m using xmlserialization to serialize data.

Here is the sample code:
C#
namespace A{
[serializable]
[DataContract(Name="User")]
[XmlAttribute("User")]
Public class User { 

[DataMember(Name="ID"]
[XmlAttribute("ID"),IsRequired=false]
public void ID {get; set;}

[DataMember(Name="ID"]
[XmlAttribute("IDSpecified"),IsRequired=false]
public void IDSpecified{get; set;}

}
}

Now I am assigning values to this properties and trying to serialize XML.
C#
User.ID=1;
User.IDSpecified=2;

It is giving error because property names are starting with same string "ID". If I change "Specified" instead of "IDSpecified" then it is serialize fine because property name is different.

So is there any rule that property name should not start with same characters.


Pls help me ASAP.

Thank you.
Posted
Updated 20-Dec-11 3:14am
v3

The error is not because you have two properties that begin with "Id". It is because both properties have the same name for the DataMember Attribute. Changing the attribute to what is below should fix your duplicate "ID" error.

C#
[DataMember(Name="IDSpecified"]
[XmlAttribute("IDSpecified"),IsRequired=false]
public void IDSpecified{get; set;}
   
hI,

Sorry for the mistake in question. Datamember names are different only.

[DataMember(Name="ID"]
[XmlAttribute("ID"),IsRequired=false]
public void ID {get; set;}

[DataMember(Name="SpecifiedID"]
[XmlAttribute("SpecifiedID"),IsRequired=false]
public void SpecifiedID{get; set;}

Still i m getting same error.
   
thanks for the reply.
I found the solution. Here it is :
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900