请帮我从WEB API响应中删除xmlns命名空间。
添加,
config.Formatters.XmlFormatter.UseXmlSerializer = true;
(或)
[DataContract(Namespace="")]
没帮我。非常感谢您的帮助。
答案 0 :(得分:8)
最后,我找到了解决方案。刚刚创建了一个CustomXmlFormatter来从根元素中删除命名空间。
public class IgnoreNamespacesXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
try
{
var task = Task.Factory.StartNew(() =>
{
var xns = new XmlSerializerNamespaces();
var serializer = new XmlSerializer(type);
xns.Add(string.Empty, string.Empty);
serializer.Serialize(writeStream, value, xns);
});
return task;
}
catch (Exception)
{
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
}
}