WCF中的Xml数据未正确序列化

时间:2015-01-06 04:15:35

标签: c# wcf rest

我一直在开发一个休息的wcf服务。这是我在服务中定义的DataContract。这是android设备用户将使用的服务,数据将以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; }
    }

以下是我定义的服务合同

 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/SaveData/New")]
        void SaveData(Employee emp);
    }

现在这项服务的实施代码如下:

 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);
                }
            }

当我使用fiddler以xml格式发送数据时,它无法正确解析。以下是我使用fiddler传递给方法的内容:

<Employee>
<ID>1</ID>
<Name>Nitin Singh</Name>
<City>
<Id>1<Id>
<CityName>New Delhi<CityName>
<City>
</Employee>

它呈现的输出如下: -

<?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>

我希望城市价值也应该存在,但它不会发生在这里。请帮我弄明白这一点。感谢

1 个答案:

答案 0 :(得分:1)

XmlSerializer允许您将列表“扁平”为一系列具有相同名称的元素,如下所示:

[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; }
}

而且,使用它:

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);
    }
}

测试代码:

    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);

对于您的类,这将生成以下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>

这就是你想要的吗?这会将XML写入字符串以进行测试。写入文件的说明如下:How to: Write Object Data to an XML File

(您在数据合同中犯了一些小错误 - public List<City> city本应为public List<City> Citypublic class City需要[DataContract(Namespace = "")]。但是,结果列表可能是两层深。)