11
votes

how can I remove the xmlns:i="http://www.w3.org/2001/XMLSchema-instance" when using DataContractSerializer.

this is what I'm getting:

<ProfileModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Email>[email protected]</Email>
  <FirstName>wolverine</FirstName>
  <ID>ty1002225</ID>
  <LastName>wolverine3</LastName>
  <PhoneNumber>66332214477</PhoneNumber>
  <SourceSystem>TY</SourceSystem>
</ProfileModel>

I want to get something like this:

<ProfileModel>
      <Email>[email protected]</Email>
      <FirstName>wolverine</FirstName>
      <ID>ty1002225</ID>
      <LastName>wolverine3</LastName>
      <PhoneNumber>66332214477</PhoneNumber>
      <SourceSystem>TY</SourceSystem>
    </ProfileModel>

this is my model:

[DataContract(Namespace = "")]
    public class CRMProfileModel
    {
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string PhoneNumber { get; set; }
        [DataMember]
        public string SourceSystem { get; set; }
        [DataMember]
        public string ID { get; set; }
    }

I'm trying to avoid to use string replace to remove it.

1
You may find your answer here: stackoverflow.com/a/8061839/3383479user3383479
@Inanikian the Op is using DataContractSerializer, not XmlSerializerBombinosh
@rcadaoas did you ever find a way to do it without string replace? I spent some time researching this, but from everything I found, it seems not possible.CrnaStena
@CrnaStena, I still went for the string replace approach unfortunately.Romeo
You need to use a custom serializer.Frère Chloé

1 Answers

0
votes

how can I remove the xmlns:i="http://www.w3.org/2001/XMLSchema-instance" when using DataContractSerializer.

  1. hii Romeo... i also tried for couple of hours to remove xmlns:i="http://www.w3.org/2001/XMLSchema-instance".

  2. Finally i found my best,hope it will helpful

    public IHttpActionResult Post([FromBody]MessageResponse value)

{

 var messageresponse =new CRMProfileModel(){.....};
DataContractSerializer doc = new  DataContractSerializer(messageresponse.GetType());  
MemoryStream ms = new MemoryStream();
 dcs.WriteObject(ms, messageresponse); 
var i = Encoding.UTF8.GetString(ms.ToArray()); 
var r = i.Replace("xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"","");
var ss = new XmlDocument(); 
ss.LoadXml(r);
return Content(HttpStatusCode.OK, ss.DocumentElement, Configuration.Formatters.XmlFormatter);

}