I have developed an ASP.NET Web API which response like this:
<ArrayOfMyData>
<MyData>
<Id>1</Id>
<Name>x</Name>
</MyData>
<MyData>
<Id>2</Id>
<Name>y</Name>
</MyData>
<MyData>
<Id>3</Id>
<Name>z</Name>
</MyData>
</ArrayOfMyData>
I want to add an attribute to the root element, so the response looks like:
<ArrayOfMyData MyAttribute="bela bela bela">
.
.
.
</ArrayOfMyData>
You would need to use XmlSerializer for this as it provides greater control over your output. By default Web API’s XmlMediaTypeFormatter uses DataContractSerializer, which does not generate attributes. To switch to XmlSerializer, you can set the flag on property: UseXmlSerializer:
Why are attributes not supported with the DataContractSerializer?