从 wcf 中删除不必要的 xsi 和 xsd 命名空间 [英] Remove unnecessary xsi and xsd namespaces from wcf

查看:30
本文介绍了从 wcf 中删除不必要的 xsi 和 xsd 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个运行良好的网络服务,只是我试图删除 xsi 和 xsd 命名空间.

I have built a web services which is working well except that I am trying to remove the xsi and xsd namespaces.

我看到很多链接表明我必须使用这样的自定义序列化程序:

I have seen a lot link showing that i have to use a custom serializer like this :

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

但我没有找到在我的代码中实现这一点的方法.这是我的代码:

but i did not find a way to implement this in my code. here is my code :

[ServiceContract, XmlSerializerFormat]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyUser
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "getUserInfo?token={userId}", BodyStyle = WebMessageBodyStyle.Bare)]
    public PersonnResponse ValidateToken(string userId)
    {
        var response = new PersonnResponse();
        response.userId = userId;
        response.firstName = myOtherServiceGetFirstName(userId);
        response.lastName = myOtherServiceGetLastName(userId);
        return response;
    }

[DataContract(Name = "person", Namespace = "")]
public class PersonnResponse
{
    [DataMember(Name = "userId", EmitDefaultValue = false)]
    public string userId { get; set; }

    [DataMember(Name = "firstName", EmitDefaultValue = false)]
    public string firstName { get; set; }

    [DataMember(Name = "lastName", EmitDefaultValue = false)]
    public string lastName { get; set; }
}

推荐答案

要获得您正在寻找的内容,您应该实施 IXmlSerializable:

To get what you're looking for you should implement IXmlSerializable:

[DataContract(Name = "person", Namespace = "")]
public class PersonnResponse:IXmlSerializable
{
   ...

     public XmlSchema GetSchema()
    {
        return null;
    }


    public void ReadXml (XmlReader reader)
    {
        var xd = XDocument.Load(reader);
        firstName = xd.Descendants().First (x => x.Name.LocalName == "firstName" ).Value;
        lastName = xd.Descendants().First (x => x.Name.LocalName == "lastName" ).Value;
        userId = xd.Descendants().First (x => x.Name.LocalName == "userId" ).Value;
    }

    public void WriteXml(XmlWriter writer){


        writer.WriteElementString("userId", userId);
        writer.WriteElementString("firstName", firstName);
        writer.WriteElementString("lastName", lastName);

    }
}
public class Test
{
    static void Main()
    {
        Test t = new Test();
        t.Serialize();
    }

    private void Serialize()
    {
        // Create an instance of the class, and an 
        // instance of the XmlSerializer to serialize it.
        var pers = new PersonnResponse(){ firstName="Call Me", lastName="Heisenberg", userId="Id"};
        XmlSerializer ser = new XmlSerializer(typeof(PersonnResponse));

        StringWriter tw = new StringWriter();
        ser.Serialize(tw,pers);
        Console.WriteLine(tw.ToString());

        //Deserialize from XML string
        var sw = new StringReader(tw.ToString());
        var NewPerson = ser.Deserialize(sw);

    }
}

您最终会得到这样的 XML:

You'll end up with XML like this:

<?xml version="1.0" encoding="utf-16"?>
<PersonnResponse>
  <userId>Id</userId>
  <firstName>Call Me</firstName>
  <lastName>Heisenberg</lastName>
</PersonnResponse>

这篇关于从 wcf 中删除不必要的 xsi 和 xsd 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆