storm_buster
storm_buster

Reputation: 7568

Remove unnecessary xsi and xsd namespaces from wcf

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; }
}

Upvotes: 0

Views: 1129

Answers (1)

Jeremy
Jeremy

Reputation: 6670

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);

    }
}

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>

Upvotes: 1

Related Questions