大家好,我需要从我的 WCF/REST 获取 XML 自定义,但我的属性名称没有按照我的意愿进行序列化,因为它包含一个冒号。
我的 DataContract 用于错误响应
[DataContract(Namespace = "xxxx:xxxx:rest", Name = "hs:rest")]
public class ErrorResult
{
[DataMember(Order = 0, Name = "status")]
string Status
{
get
{
return "ERROR(5698)";
}
set { }
}
[DataMember(Order = 1, Name = "reason")]
public string Reason { get; set; }
[DataMember(Order = 2, Name = "extendedMessage")]
public string ExtendedMessage { get { return string.Empty; } set { } }
[DataMember(Order = 3, Name = "result")]
public string Result { get { return string.Empty; } set { } }
[DataMember(Order = 4, Name = "type")]
public string Type { get { return string.Empty; } set { } }
[...]
}
我的序列化器
public string XmlTranslation(ErrorResult error)
{
using (var memoryStream = new MemoryStream())
{
using (var reader = new StreamReader(memoryStream))
{
var serializer = new DataContractSerializer(error.GetType());
serializer.WriteObject(memoryStream, error);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
使用这些方法并分析与提琴手的网络通信,结果如下:
要求:(不重要)
<?xml version="1.0" encoding="UTF-8"?><rest xmlns="xxxx:xxxx:rest" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance ">
<type>date</type>
<domain>test.it</domain>
<date>2009-03-13</date>
<page>1</page>
<reportType>daily</reportType>
<actionName>store</actionName>
</rest>
回复:
<hs_x003A_rest xmlns="xxxx:xxxx:rest" xmlns:i="http://www.w3.org/2001/XMLSchema-
instance">
<status>ERROR(5698)</status>
<reason>InternalError(1011)</reason>
<extendedMessage/>
<result/>
<type/>
</hs_x003A_rest>
我得到一个像“hs_x003A_rest”这样的名字,但我得到“hs:rest”你有什么建议吗?提前感谢您的回答