Question

I have a predefined xml sample which defines the requests and responses, the only part I can't get working with ServiceStack.Text.XmlSerializer is the following snippet, which is basically a list of strings.

<user>
....
    <EmailPreferences> 
        <EmailProgram>Newsletter</EmailProgram> 
        <EmailProgram>Coupons</EmailProgram> 
    </EmailPreferences> 

I tried using the example Using Structs to customise JSON, but as the title implies that didn't affect the xml serialisation.

Était-ce utile?

La solution

ServiceStack uses .NET's XML DataContractSerializer under the hood. So you can decorate the models with any customizations it support. So to get something like the above you could do:

[CollectionDataContract(Name="EmailPreferences", ItemName="EmailProgram")]
public class EmailPreferences : List<string>
{
    public EmailPreferences() { }
    public EmailPreferences(IEnumerable<string> collection) : base(collection){}
}

Global XML Namespaces

Although you can individually add namespaces to each DataContract a better idea instead is to have all your DTOs share the same namespace, this will prevent the auto-generated and repeating namespaces from appearing in your XML.

As the ResponseStatus DTO is already under http://schemas.servicestack.net/types namespace so if you don't care what your namespace is I would leave it at that.

The easiest way to have all your DataContract's under the same namespace is to put these assembly wide attributes in your AssemblyInfo.cs for each C# namespace your DTOs are in:

[assembly: ContractNamespace("http://schemas.servicestack.net/types",  
    ClrNamespace = "ServiceStack.Examples.ServiceModel.Operations")]
[assembly: ContractNamespace("http://schemas.servicestack.net/types",  
   ClrNamespace = "ServiceStack.Examples.ServiceModel.Types")]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top