12

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>wolverine@wolverine.com</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>wolverine@wolverine.com</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.

Romeo
  • 1,791
  • 8
  • 25
  • 41
  • You may find your answer here: http://stackoverflow.com/a/8061839/3383479 –  Apr 16 '14 at 08:43
  • @Inanikian the Op is using DataContractSerializer, not XmlSerializer – Bombinosh Apr 15 '15 at 15:34
  • @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 May 29 '15 at 13:22
  • @CrnaStena, I still went for the string replace approach unfortunately. – Romeo May 31 '15 at 09:49
  • You need to use a custom serializer. – Frère Chloé Jul 27 '16 at 14:13
  • Can you add to your post your implementation of serialization CRMProfileModel class with DataContractSerializer? – SLenik Nov 17 '16 at 12:59
  • Possible duplicate of [Removing namespaces serializing with DataContractSerializer](https://stackoverflow.com/questions/31901937/removing-namespaces-serializing-with-datacontractserializer) – Neil Jun 28 '18 at 16:08

1 Answers1

-1

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

}

mahi
  • 1
  • 1
  • 4
    the OP asks for a solution other than 'replace': **I'm trying to avoid to use string replace to remove it.** – Felix D. Feb 23 '17 at 10:13