Web Api XML, How to Set Encoding, Version, xmlns: xsi and xsi: schemaLocation

I am using asp.net MVC4 Web Api.

I have installed:

Dim xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter xml.UseXmlSerializer = True 

I created a class that specifies the XML I need, and it works well.

I'm almost there, but I'm not sure how to install:

 <?xml version="1.0" encoding="utf-8"?> 

and how to set element attributes:

xmlns: xsi and xsi: schemaLocation

Can I set this with an attribute?

+6
source share
1 answer

This response is delayed for one year and tested on WebAPI2!

Enable XML Declaration in WebApiConfig

 config.Formatters.XmlFormatter.WriterSettings.OmitXmlDeclaration = false; 

Then add the schemaLocation property or member (I always prefer the property)

 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"; } } 

Conclusion:

 <?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> 
+5
source

All Articles