C # WebAPI sérialiser une liste de chaînes

Cela devrait être une question relativement facile. Je déroupe en ligne depuis un moment et je ne trouve toujours pas de solution.

En ce moment, mon webapi renvoie une sortie comme celle-ci

  Japanese Korean French   

Je veux que ça retourne comme ça

   Japanese Korean French   

Quel est le moyen le plus simple d’accomplir une telle tâche?

Donc, fondamentalement, il y a deux choses que je veux faire

1) Supprimez les espaces de noms xmlns: d3p1 = “http://schemas.microsoft.com/2003/10/Serialization/Arrays” 2) modifiez le nom de l’élément outter de

  

à

  

3) Modifier le nom de l’élément interne de

  

à

  

Et mon membre dans la classe Merchant est comme ça

 [DataMember(EmitDefaultValue = false)] public List WebCuisine { get; set; } 

Merci d’avance

Vous devez utiliser votre propre sérialiseur.

  1. Créer une structure de données

     [XmlRoot("Merchant")] public class Merchant { [XmlArray("Cuisines"), XmlArrayItem("Cuisine")] public List WebCuisine { get; set; } } 
  2. Créer une classe héritée de 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(); } } 

Comme vous pouvez le constater, je ne suis intéressé que par l’écriture, mais pas pour la lecture Cependant, vous pouvez facilement implémenter ReadObject si vous en avez besoin.

  1. Ensuite, dans WebApiConfig, dans public static void Register(HttpConfiguration config) vous ajoutez

      config.Formatters.XmlFormatter.SetSerializer(new MerchantSerializer()); 

Et vous devriez avoir

   Japanese Korean French   

Je ne sais pas si cela aidera quelqu’un mais j’ai pris le sérialiseur marchand et je l’ai modifié en un sérialiseur générique.

 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 

}

Ensuite, tout ce que vous avez à faire est d’enregistrer les types de votre choix pour utiliser cet serializser:

 // Set the Serializer for certain objects GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(serializer); GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(serializer);