2

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

4
  • Your data model shows a list of cities but your XML only shows one city. Can there be multiple cities? Commented Jan 6, 2015 at 4:31
  • yes there can be multiple list in it n thats the problem i m facing the city data is not coming in the xml that is resulting Commented Jan 6, 2015 at 4:39
  • Can you switch to XmlSerializer? Commented Jan 6, 2015 at 4:41
  • Can you provide some kind of implementation of XmlSerializer in this scenario. It would be really helpfull Commented Jan 6, 2015 at 4:43

1 Answer 1

1

XmlSerializer allows you to "flatten" a list into a sequence of identically named elements, like so:

[XmlRoot("Employee", Namespace="")]
public class Employee
{
    public int ID { get; set; }

    public string Name { get; set; }

    [XmlElement("City")]
    public List<City> City { get; set; }
}

public class City
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string CityName { get; set; }
}

And, to use it:

public static class XmlSerializationHelper
{
    public static string GetXml<T>(T obj, XmlSerializer serializer, bool omitStandardNamespaces)
    {
        using (var textWriter = new StringWriter())
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;        // For cosmetic purposes.
            settings.IndentChars = "    "; // For cosmetic purposes.
            using (var xmlWriter = XmlWriter.Create(textWriter, settings))
            {
                if (omitStandardNamespaces)
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
                    serializer.Serialize(xmlWriter, obj, ns);
                }
                else
                {
                    serializer.Serialize(xmlWriter, obj);
                }
            }
            return textWriter.ToString();
        }
    }

    public static string GetXml<T>(this T obj, bool omitNamespace)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        return GetXml(obj, serializer, omitNamespace);
    }

    public static string GetXml<T>(this T obj)
    {
        return GetXml(obj, false);
    }
}

Test code:

    var employee = new Employee { Name = "Nitin Singh", ID = 1, City = new[] { new City { CityName = "New Delhi", Id = 1 }, new City { CityName = "Bangalore", Id = 2 } }.ToList() };
    var xml = employee.GetXml();

    Debug.WriteLine(xml);

For your classes this produces the following XML:

<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ID>1</ID>
    <Name>Nitin Singh</Name>
    <City>
        <Id>1</Id>
        <CityName>New Delhi</CityName>
    </City>
    <City>
        <Id>2</Id>
        <CityName>Bangalore</CityName>
    </City>
</Employee>

Is that what you want? This writes the XML to a string for testing purposes. Instructions for writing to a file are here: How to: Write Object Data to an XML File.

(You made a couple minor mistakes with your data contract -- public List<City> city should have been public List<City> City and public class City needed [DataContract(Namespace = "")]. However, the resulting list would have been two levels deep.)

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

Comments

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.