本文介绍了如何去除WEB API响应命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮我从Web API响应删除的xmlns命名空间。
Please help me to remove xmlns namespace from the WEB API Response.
添加
config.Formatters.XmlFormatter.UseXmlSerializer = true;
(或)
[DataContract(Namespace="")]
我没有帮助。你的帮助是非常AP preciated。
didn't help me. Your help is much appreciated.
推荐答案
最后,我找到了解决办法。刚刚创建了一个CustomXmlFormatter从根元素中删除的命名空间。
Finally, I found the solution. Just created a CustomXmlFormatter to remove the namespace from the root element.
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);
}
}
}
这篇关于如何去除WEB API响应命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!