i have been developing a wcf service with rest. Here is my DataContract that i have defined in the service. This is service will be consumed by the android device users and the data will be get passed in the service method in the form of the xml.
[DataContract(Namespace = "")]
public class Employee
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public List<City> city { get; set; }
}
[DataContract]
public class City
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string CityName { get; set; }
}
And the following is the servicecontract that i have defined
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/SaveData/New")]
void SaveData(Employee emp);
}
Now the implementation code for this service is as follows :
public void SaveData(Employee emp)
{
Employee obj = emp;
DataContractSerializer dcs = new DataContractSerializer(typeof(Employee));
using (Stream stream = new FileStream(@"D:\file.xml", FileMode.Create, FileAccess.Write))
{
using (XmlDictionaryWriter writer =
XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8))
{
writer.WriteStartDocument();
dcs.WriteObject(writer, obj);
}
}
When i send the data in xml format using fiddler it is not getting parsed correctly. Here is what i m passing to the method using fiddler :
<Employee>
<ID>1</ID>
<Name>Nitin Singh</Name>
<City>
<Id>1<Id>
<CityName>New Delhi<CityName>
<City>
</Employee>
the output that it is rendering is as follows : -
<?xml version="1.0" encoding="utf-8"?><Employee xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ID>1</ID><Name>Nitin Singh</Name><city i:nil="true" xmlns:a="http://schemas.datacontract.org/2004/07/SampleService"/></Employee>
I want the city valus should also be present into but it is not happening here. Kindly help me to figure out this. Thanks
XmlSerializer?