我有一个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 Class
的副本[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; }
}
我的mapper类如下:
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:”。如果你看到MessageResponse类(Status标签)正确,但MessageResponse中的数组列表(即:Message [])出错了。
提前致谢。
答案 0 :(得分:1)
a
是元素的生成命名空间。
对于DataContractSerializer,您可以使用ServiceContractAttribute
删除服务定义中类型的命名空间,如下所示:
[ServiceContract(Namespace="")]