Xml WCF 中的数据未正确序列化
Xml Data In WCF Not Serializing Correctly
我一直在开发一个 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
[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>
我希望 city valus 也应该存在于其中,但它没有发生在这里。请帮我弄清楚这一点。谢谢
XmlSerializer
允许您将列表 "flatten" 转换为一系列同名元素,如下所示:
[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> City
而 public class City
需要 [DataContract(Namespace = "")]
。但是,结果列表将有两层深。)
我一直在开发一个 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
[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>
我希望 city valus 也应该存在于其中,但它没有发生在这里。请帮我弄清楚这一点。谢谢
XmlSerializer
允许您将列表 "flatten" 转换为一系列同名元素,如下所示:
[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> City
而 public class City
需要 [DataContract(Namespace = "")]
。但是,结果列表将有两层深。)