调整MVC 4的WebAPI的XmlSerializer失去命名空间 [英] Adjust MVC 4 WebApi XmlSerializer to lose the nameSpace

查看:407
本文介绍了调整MVC 4的WebAPI的XmlSerializer失去命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个MVC的WebAPI,使用EF与POCO类进行存储。
我想要做的就是摆脱从XML命名空间,使端点将没有它返回并接受XML对象。 (JSON工作得很好)

I'm working on a MVC WebAPI, that uses EF with POCO classes for storage. What I want to do is get rid of the namespace from the XML, so that the endpoints would return and accept xml objects without it. (json works just fine)

<ACCOUNT xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Platform.Services.AccountService.Data">
<id>22</id>
<City i:nil="true"/>
<Country i:nil="true"/>
<Email>testas@email.com</Email>
<Phone i:nil="true"/> ...

我想这个工作。

 <ACCOUNT>
    <id>22</id>
    <City i:nil="true"/>
    <Country i:nil="true"/>
    <Email>testas@email.com</Email>
    <Phone i:nil="true"/> ...

希望无需装饰POCO的一束属性。

Hopefully without having to decorate the POCO's with a bunch of attributes.

我已经设置了这样的测试解决方案,而事实上,这些方法是beeing命中(必须是在我的系统中的一些其他问题)。反正 - 我得到使用此解决方案的结果是这样的:

I've set up a test solution for this, and indeed, these methods are beeing hit (must be some other problem in my system). Anyways - the result that I get using this solutions is this:

<ArrayOfAccount>
<Account>
<id>22</id>
<name>TestAcc</name>
<parentid xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:nil="true"/>
<status_id xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance" d3p1:nil="true"/>
<Email>Test@Test.com</Email>
</Account>
</ArrayOfAccount>

摆脱顶部的模式,但现在的属性都搞砸了:(
这里有一个样本项目中的链接

推荐答案

这答案这里是标记斑点<一个href=\"http://stackoverflow.com/questions/12590801/remove-namespace-in-xml-from-asp-net-mvc-web-api\">Remove命名空间的ASP.NET MVC的Web API 的XML。\\

This answer here is spot on the mark Remove namespace in XML from ASP.NET MVC Web API.\

如果你不想来装饰你的POCO的所有使用第一种选择:

If you don't want to decorate your POCO's at all use the 1st option:

config.Formatters.XmlFormatter.UseXmlSerializer = true;

如果您使用选项2,您可能需要添加一个引用 System.Runtime.Serialization

If you use option 2, you may need to add a reference to System.Runtime.Serialization

假设这样一个帖子接受并正确设置:

Assuming a post like this with Accept set correct:

GET HTTP://任何旧的服务器/ API / FOOS / 5
接受:应用/ XML

GET http:// ANY OLD SERVER/api/foos/5 Accept: application/xml

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Web.Http;

namespace CutomXmlFormater.Controllers
{
//[DataContract(Namespace = "")]
public class Foo
{
    //[DataMember]
    public string Bar { get; set; }
}

public class FoosController : ApiController
{
    // GET api/foos/5
    public Foo Get(int id)
    {
        return new Foo() { Bar = "Test" };
    }
}

}

配置(App_Start / WebApiConfig)

//(Use this is you don't go the data contact and model annotation route)
config.Formatters.XmlFormatter.UseXmlSerializer = true;

结果

无论是(有注释和数据触点):

Either (With annotation and data contact):

<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Bar>Test</Bar></Foo>

或(使用XML串行器路线):

Or (with XML serialiser route):

<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Bar>Test</Bar></Foo>

这篇关于调整MVC 4的WebAPI的XmlSerializer失去命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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