c# - Property will not serialize as XML attribute -


i attempting serialize class xml , have properties serialized attributes of class, rather nested node. using webapi automatically handle serialization of xml.

this class:

[datacontract (namespace="", name="attributetest")] [serializable] public class attributetestclass {     [xmlattribute("property")]     [datamember]     public int property1 { get; set; } } 

here output receiving (note property1 not attribute in spite of being decorated [xmlattribute]):

<attributetest xmlns:i="http://www.w3.org/2001/xmlschema-instance"> <property1>123</property1> </attributetest> 

this output want receive:

<attributetest property1="123" xmlns:i="http://www.w3.org/2001/xmlschema-instance"> </attributetest> 

what missing?

i'm not familiar webapi output receive looks it's serialized using datacontractserializer, not xmlserializer need. check if adding following application_start in global.asax helps:

globalconfiguration.configuration.formatters.clear(); globalconfiguration.configuration.formatters.add(     new system.net.http.formatting.xmlmediatypeformatter()); globalconfiguration.configuration.formatters.xmlformatter.usexmlserializer = true; 

(from http://serena-yeoh.blogspot.de/2013/02/xml-serialization-in-aspnet-web-api.html)


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -