首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web Api XML,如何设置编码,版本,xmlns:xsi和xsi:schemaLocation

Web Api XML,如何设置编码,版本,xmlns:xsi和xsi:schemaLocation
EN

Stack Overflow用户
提问于 2012-10-28 20:52:11
回答 1查看 3K关注 0票数 6

我正在使用asp.net MVC4 Web Api。

我已经设置了:

代码语言:javascript
复制
Dim xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter
xml.UseXmlSerializer = True

我已经创建了一个类,它指定了我需要的XML,并且运行良好。

我就快到了,但我不确定如何设置:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

以及如何设置元素属性:

xmlns:xsi和xsi:schemaLocation

我可以使用一个属性来设置它吗?

EN

回答 1

Stack Overflow用户

发布于 2014-04-14 22:39:34

这个答案延迟了一年,并针对WebAPI2进行了测试!

WebApiConfig类中启用XML声明

代码语言:javascript
复制
config.Formatters.XmlFormatter.WriterSettings.OmitXmlDeclaration = false;

然后添加schemaLocation属性或成员(我总是更喜欢属性)

代码语言:javascript
复制
public class SampleData
{
    [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string SchemaLocation { get; set; }

    //other properties
    public string Prop1 { get; set; }

    public SampleData()
    {
        SchemaLocation = "http://localhost/my.xsd";
    }
}

输出:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<TestModel 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://localhost/my.xsd">
    <Prop1>1</Prop1>
</TestModel>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13108934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档