C#WebAPI序列化字符串列表
这应该是一个相对简单的问题,我在网上暂时待了一段时间,仍然无法找到解决方案。
现在我的webapi返回这样的输出
Japanese Korean French
我希望它像这样回归
Japanese Korean French
完成这项任务的最简单方法是什么?
所以基本上我有两件事要做
1)摆脱命名空间xmlns:d3p1 =“http://schemas.microsoft.com/2003/10/Serialization/Arrays”2)更改outter元素的名称
至
3)更改内部元素的名称
至
我在Merchant类中的数据库是这样的
[DataMember(EmitDefaultValue = false)] public List WebCuisine { get; set; }
谢谢你在advnace
您必须使用自己的序列化程序。
-
创建数据结构
[XmlRoot("Merchant")] public class Merchant { [XmlArray("Cuisines"), XmlArrayItem("Cuisine")] public List
WebCuisine { get; set; } } -
创建一个inheritance自
XmlObjectSerializer
的类public class MerchantSerializer : XmlObjectSerializer { XmlSerializer serializer; public MerchantSerializer() { this.serializer = new XmlSerializer(typeof(Merchant)); } public override void WriteObject(XmlDictionaryWriter writer, object graph) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); serializer.Serialize(writer, graph, ns); } public override bool IsStartObject(XmlDictionaryReader reader) { throw new NotImplementedException(); } public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName) { throw new NotImplementedException(); } public override void WriteEndObject(XmlDictionaryWriter writer) { throw new NotImplementedException(); } public override void WriteObjectContent(XmlDictionaryWriter writer, object graph) { throw new NotImplementedException(); } public override void WriteStartObject(XmlDictionaryWriter writer, object graph) { throw new NotImplementedException(); } }
正如你所看到的,我只对写作感兴趣,而不是阅读。 但是,如果需要,可以轻松实现ReadObject。
-
在
public static void Register(HttpConfiguration config)
中的WebApiConfig之后添加config.Formatters.XmlFormatter.SetSerializer
(new MerchantSerializer());
你应该得到
Japanese Korean French
我不知道这是否会对任何人有所帮助,但我采用了Merchant Serializer并将其修改为Generic Serializer
using System; using System.Runtime.Serialization; using System.Xml; using System.Xml.Serialization; namespace NoNamespaceXml { public class GenericSerializer : XmlObjectSerializer { #region Private Variables private XmlSerializer serializer; #endregion #region Constructor /// /// Create a new instance of a GenericSerializer /// /// public GenericSerializer (object objectToSerialize) { // If the objectToSerialize object exists if (objectToSerialize != null) { // Create the Serializer this.Serializer = new XmlSerializer(objectToSerialize.GetType()); } } #endregion #region Methods #region IsStartObject(XmlDictionaryReader reader) /// /// This method Is Start Object /// public override bool IsStartObject(XmlDictionaryReader reader) { throw new NotImplementedException(); } #endregion #region ReadObject(XmlDictionaryReader reader, bool verifyObjectName) /// /// This method Read Object /// public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName) { throw new NotImplementedException(); } #endregion #region WriteEndObject(XmlDictionaryWriter writer) /// /// This method Write End Object /// public override void WriteEndObject(XmlDictionaryWriter writer) { throw new NotImplementedException(); } #endregion #region WriteObject(XmlDictionaryWriter writer, object graph) /// /// This method Write Object /// public override void WriteObject(XmlDictionaryWriter writer, object graph) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); serializer.Serialize(writer, graph, ns); } #endregion #region WriteObjectContent(XmlDictionaryWriter writer, object graph) /// /// This method Write Object Content /// public override void WriteObjectContent(XmlDictionaryWriter writer, object graph) { throw new NotImplementedException(); } #endregion #region WriteStartObject(XmlDictionaryWriter writer, object graph) /// /// This method Write Start Object /// public override void WriteStartObject(XmlDictionaryWriter writer, object graph) { throw new NotImplementedException(); } #endregion #endregion #region Properties #region HasSerializer /// /// This property returns true if this object has a 'Serializer'. /// public bool HasSerializer { get { // initial value bool hasSerializer = (this.Serializer != null); // return value return hasSerializer; } } #endregion #region Serializer /// // This property gets or sets the value for 'Serializer'. /// public XmlSerializer Serializer { get { return serializer; } set { serializer = value; } } #endregion #endregion } #endregion
}
然后,您只需注册要使用此序列化器的任何类型:
// Set the Serializer for certain objects GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(serializer); GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(serializer);