调整 MVC 4 WebApi XmlSerializer 以丢失 nameSpace [英] Adjust MVC 4 WebApi XmlSerializer to lose the nameSpace
问题描述
我正在开发一个 MVC WebAPI,它使用带有 POCO 类的 EF 进行存储.我想要做的是从 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.
我已经为此设置了一个测试解决方案,确实,这些方法受到了打击(一定是我系统中的其他问题).无论如何 - 我使用这个解决方案的结果是这样的:
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>
摆脱了顶部的架构,但现在属性一团糟:(这是示例项目
Got rid of the schema on top, but the properties are now messed up :( Here's a link to a sample project
推荐答案
这里的答案很明显 从 ASP.NET Web API 中删除 XML 中的命名空间.
如果您根本不想装饰您的 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:
获取 http://任何旧服务器/api/foos/5接受:application/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 以丢失 nameSpace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!