请帮我从WEB API响应中删除xmlns命名空间.
添加,
config.Formatters.XmlFormatter.UseXmlSerializer = true;
Run Code Online (Sandbox Code Playgroud)
(要么)
[DataContract(Namespace="")]
Run Code Online (Sandbox Code Playgroud)
没帮我 非常感谢您的帮助.
最后,我找到了解决方案.刚刚创建了一个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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2611 次 |
| 最近记录: |