WCF服务响应中意外的标记名
本文关键字:意外 服务 响应 WCF | 更新日期: 2023-09-27 18:18:18
我有一个wcf服务,我有如下类:
public class Message
{
[XmlElement(ElementName = "message")]
[DataMember(Name = "message")]
public string message { get; set; }
[XmlElement(ElementName = "MsgID")]
[DataMember(Name = "MsgID")]
public string MsgID{ get; set; }
}
然后我在浏览器中浏览了我的服务,我得到的XML如下:
<MessageResponse>
<Status>SUCCESS</Status>
<Messages>
<a:Message>
<a:message>msg1</a:message>
<a:MsgID>1</a:MsgID>
</a:Message>
</Messages>
</MessageResponse>
我的服务合同如下:更重要的是:MessageDco类是Message类
的副本。[ServiceContract(Namespace="")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserLoginProfileService
{
[WebGet(UriTemplate = "/GetMessages?MsgID={MsgID}")]
public MessageResponse GetMessage(Guid MsgID)
{
MessageResponse objMessageResponse = new MessageResponse();
try
{
Message[] objMessage = new MessageFacade().GetMessage(MsgIDs);
if (objUserLoginProfile != null)
{
MessageResponse.Status = Status.Success;
MessageResponse.Messages =Mapper.ToDataContractObjects(objMessage);
}
else
{
objMessageResponse.Status = Status.Success;
}
}
catch (Exception ex)
{
objMessageResponse.Status = Status.Failure;
}
return objMessageResponse;
}
}
public class MessageResponse
{
[XmlElement(ElementName = "Messages")]
[DataMember(Name = "Messages")]
public MessageDco[] Messages { get; set; }
[XmlElement(ElementName = "Status")]
[DataMember(Name = "Status")]
public string Status { get; set; }
}
我的映射器类如下所示:
public sealed class Mapper
{
public static MessageDco[] ToDataContractObjects(IEnumerable<MessageDco> objMessageDco)
{
if (objMessageDco != null)
{
return objMessageDco.Select(a => ToDataContractObjects(a)).ToArray();
}
else
{
return null;
}
}
}
如果我们看到"a:"被添加到标签中。为什么要加呢?我无法从标签中删除"a:"。请帮助我如何从标签中删除"a:"。如果你看到messagerresponse类(状态标签)是正确的,但是数组列表(i。e: Message[]) in messagerresponse is Coming wrong.
a
是为元素生成的命名空间。
对于DataContractSerializer,您可以使用ServiceContractAttribute
在服务定义中删除类型的名称空间,如下所示:
[ServiceContract(Namespace="")]