ServiceStack输出XML格式 - xml属性

时间:2018-03-10 12:07:03

标签: c# xml servicestack servicestack-text

我正在使用ServiceStack,需要以特定格式呈现XML。

这是我的POCO课程

<LookupModelBase Id="1">
   <Label>Label 1</Label>
</LookupModelBase>

预期产量应如下所示。

<LookupModelBase>
   <Id>1</Id>
   <Label>Label 1</Label>
</LookupModelBase>

但是我得到的输出如下

var product = {
  '123':{
    'name':'Basket',
    'price':'12.30'
  }
}

var id = 123;

var basket_name = product[id].name; 
alert(basket_name);
var basket_price = product[id].name; 
alert(basket_price);

如何解决此问题。

2 个答案:

答案 0 :(得分:0)

ServiceStack使用.NETs XML DataContractSerializer,它只允许通过.NETs DataContract属性进行自定义,而不是您正在使用的XmlSerializer属性。

答案 1 :(得分:0)

我解决了,

public static class CustomServiceStackXmlFormat
{
    public static string Format = "application/xml";

    public static void Serialize(IRequest req, object response, Stream stream)
    {
        System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(response.GetType());
        xmlSerializer.Serialize(stream, response);
    }

    public static object Deserialize(Type type, Stream stream)
    {
        System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type.GetType());
        var obj = ((Type)xmlSerializer.Deserialize(stream));
        return obj;
    }
}

AppHost.cs

this.ContentTypes.Register(CustomServiceStackXmlFormat.Format, CustomServiceStackXmlFormat.Serialize, CustomServiceStackXmlFormat.Deserialize);