如果返回格式为xml,如何在Web API中删除架构节点? [英] How to remove schema node in web api if return format is xml?

查看:109
本文介绍了如果返回格式为xml,如何在Web API中删除架构节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web api方法,它以format作为参数来提供返回xml和json的方法.该方法返回的数据类型是DataTable.json格式的一切看起来都很好,但是xml格式的datatable和其他一些属性在xml节点中也返回.如何返回仅包含数据表数据的简单xml?此外,我在WebApiConfig中使用QueryStringMapping.

I have a web api method that takes format as a parameter that provides returning both xml and json.The data type that method return is DataTable.In json format everything looks fine but in xml format the schema of datatable and some other attributes in xml nodes also returning.How to return simple xml that includes only data of datatable?Also, I am using QueryStringMapping in WebApiConfig.

这是WebApiConfig代码

This is WebApiConfig Code

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("format", "json", new MediaTypeHeaderValue("application/json")));
    config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("format", "xml", new MediaTypeHeaderValue("application/xml")));
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
}

这是控制器方法的伪代码

This is a pseudo code of controller method

[BasicAuthentication]
[Route("api/{tablename}")]
[HttpGet]
public IHttpActionResult Get(string tablename, string orders = "", int limit = 100)
{
    DataTable dt = new DataTable{TableName="resource"};
    //... Database connection and getting result
    return Ok(new Response{ limit = limit,count=dt.Rows.Count, data =dt });
}

和响应模型

public class Response
{
    public int limit { get; set; }
    public int count { get; set; }
    public DataTable data { get; set; }

}

返回的xml的示例

   <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <limit>1</limit>
    <count>1</count>
    <data>
    <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="resource" msdata:UseCurrentLocale="true">
    <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="resource">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="ID" type="xs:long" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <DocumentElement>
    <resource diffgr:id="resource1" msdata:rowOrder="0">
    <ID>1</ID>
    </resource>
    </DocumentElement>
    </diffgr:diffgram>
    </data>
    </Response>

总而言之,我只想返回数据节点中没有任何属性的资源节点.

To sum up, I want to return only resource nodes in data node without any attribute.

推荐答案

发布此问题后,我找到了答案.问题是数据表对象的XmlSchema.编写自定义数据表xml序列化器,并在IXmlSerializable接口的GetSchema中返回null并覆盖它.自定义序列化器是那里.

I have found the answer after posting this question. The problem is XmlSchema of datatable object. Writing custom datatable xml serializer with returning null in GetSchema of IXmlSerializable interface with overriding it. The custom serializer is there.

这篇关于如果返回格式为xml,如何在Web API中删除架构节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆