我正在使用asp.net MVC4 Web Api。
我已经设置了:
Dim xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter
xml.UseXmlSerializer = True我已经创建了一个类,它指定了我需要的XML,并且运行良好。
我就快到了,但我不确定如何设置:
<?xml version="1.0" encoding="utf-8"?>以及如何设置元素属性:
xmlns:xsi和xsi:schemaLocation
我可以使用一个属性来设置它吗?
发布于 2014-04-14 22:39:34
这个答案延迟了一年,并针对WebAPI2进行了测试!
在WebApiConfig类中启用XML声明
config.Formatters.XmlFormatter.WriterSettings.OmitXmlDeclaration = false;然后添加schemaLocation属性或成员(我总是更喜欢属性)
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";
}
}输出:
<?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>https://stackoverflow.com/questions/13108934
复制相似问题