J'ai trouvé une bonne solution à ce problème qui vous permet d'injecter votre XmlSerializer dans WCF qui est utilisé lors de la sérialisation et désérialisation demandes. Ce XmlSerializer peut être configuré pour omettre complètement les espaces de noms XML (y compris xmlns:i="w3.org/2001/XMLSchema-instance"
) ou de toute autre manière.
Ma solution utilise WcfRestContrib. Vous pourriez presque utiliser le POX formatter inclus mais dans notre cas, nous voulions prendre en charge les attributs, nous avons donc écrit notre propre formateur simple.
Instructions:
1) Référence WcfRestContrib de votre projet.
2) Créer une IWebFormatter
mise en œuvre:
public class NamespacelessXmlFormatter : IWebFormatter {
public object Deserialize(WebFormatterDeserializationContext context, Type type) {
if (context.ContentFormat != WebFormatterDeserializationContext.DeserializationFormat.Xml) {
throw new InvalidDataException("Data must be in xml format.");
}
return NamespacelessXmlSerializer.Deserialize(context.XmlReader, type);
}
public WebFormatterSerializationContext Serialize(object data, Type type) {
using (var stream = NamespacelessXmlSerializer.Serialize(data, type)) {
using (var binaryReader = new BinaryReader(stream)) {
byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
return WebFormatterSerializationContext.CreateBinary(bytes);
}
}
}
}
qui utilise un XmlSerializer qui correspond à vos besoins (est ici la nôtre qui omet simplement tous les espaces de noms):
public static class NamespacelessXmlSerializer {
private static readonly XmlSerializerNamespaces _customNamespace = new XmlSerializerNamespaces();
private static readonly XmlWriterSettings _xmlSettings = new XmlWriterSettings {
OmitXmlDeclaration = true
};
static NamespacelessXmlSerializer() {
// to make sure .NET serializer doesn't add namespaces
_customNamespace.Add(String.Empty, String.Empty);
}
/// <summary>
/// Deserializes object from its XML representation.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream"></param>
/// <returns></returns>
public static T Deserialize<T>(Stream stream) {
return (T)Deserialize(stream, typeof(T));
}
/// <summary>
/// Deserializes object from its XML representation.
/// </summary>
public static object Deserialize(Stream stream, Type type) {
var ds = new XmlSerializer(type);
var d = ds.Deserialize(stream);
return d;
}
public static object Deserialize(XmlDictionaryReader xmlReader, Type type) {
var ds = new XmlSerializer(type);
var d = ds.Deserialize(xmlReader);
return d;
}
/// <summary>
/// Serializes object to XML representation.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Is thrown when there was an error generating XML document. This can happen
/// for example if the object has string with invalid XML characters:
/// http://www.w3.org/TR/2004/REC-xml-20040204/#charsets.
/// See this article for other potential issues:
/// http://msdn.microsoft.com/en-us/library/aa302290.aspx
/// </exception>
public static Stream Serialize<T>(T objectToSerialize) {
return Serialize(objectToSerialize, typeof(T));
}
/// <summary>
/// Serializes object to XML representation.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Is thrown when there was an error generating XML document. This can happen
/// for example if the object has string with invalid XML characters:
/// http://www.w3.org/TR/2004/REC-xml-20040204/#charsets.
/// See this article for other potential issues:
/// http://msdn.microsoft.com/en-us/library/aa302290.aspx
/// </exception>
public static Stream Serialize(object objectToSerialize, Type type) {
var stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream, _xmlSettings);
var x = new XmlSerializer(type);
x.Serialize(writer, objectToSerialize, _customNamespace);
stream.Position = 0;
return stream;
}
}
3) Appliquer les WebDispatchFormatter...
attributs à votre service utilisant votre implémentation personnalisée comme type (basé sur ce documentation):
[WebDispatchFormatterConfiguration("application/xml")]
[WebDispatchFormatterMimeType(typeof(NamespacelessXmlFormatter), "application/xml")]
4) Appliquer l'attribut WebDispatchFormatter
à toutes vos méthodes de service (sur la base de ce documentation).
5) C'est tout. Testez votre service et confirmez qu'il se comporte maintenant comme prévu.
Ce serait bien si le downvoter laisserait un commentaire. Ma réponse est 100% correcte et réalisable. Il ne correspond peut-être pas exactement à ce que la personne demande, ou peut-être y a-t-il un meilleur moyen, mais cela fonctionne certainement. –
Je n'étais pas le downvoter, mais bien que votre réponse soit "correcte", elle ne répond pas à cette question particulière. –