当前位置: 首页 > 知识库问答 >
问题:

删除xmlns:i=”http://www.w3.org/2001/XMLSchema-instance“使用DataContractSerializer时

夏侯弘光
2023-03-14

如何删除xmlns:I=”http://www.w3.org/2001/XMLSchema-instance“使用DataContractSerializer时。

这就是我得到的:

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

我想得到这样的东西:

<ProfileModel>
      <Email>wolverine@wolverine.com</Email>
      <FirstName>wolverine</FirstName>
      <ID>ty1002225</ID>
      <LastName>wolverine3</LastName>
      <PhoneNumber>66332214477</PhoneNumber>
      <SourceSystem>TY</SourceSystem>
    </ProfileModel>

这是我的模型:

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

我试图避免使用字符串替换来删除它。

共有1个答案

太叔豪
2023-03-14

如何删除xmlns:I=”http://www.w3.org/2001/XMLSchema-instance“使用DataContractSerializer时。

>

  • 嗨,罗密欧……我也试了几个小时来删除xmlns:i=”http://www.w3.org/2001/XMLSchema-instance".

    最后我找到了我最好的,希望会有帮助

    公共IHttpActionResult帖子([FromBody]消息响应值)

    {

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

    }

  •  类似资料: