Delete namespace from DataContract not working

I need two simple serialize / desirialize methods,

Mapping:

[System.Runtime.Serialization.DataContract(Namespace = "", Name = "PARAMS")] public sealed class CourseListRequest { [DataMember(Name = "STUDENTID")] public int StudentId { get; set; } [DataMember(Name = "YEAR")] public string Year { get; set; } [DataMember(Name = "REQUESTTYPE")] public int RequestType { get; set; } } public static string Serialize<T>(this T value) { if (value == null) throw new ArgumentNullException("value"); try { var dcs = new DataContractSerializer(typeof (T)); string xml; using (var ms = new MemoryStream()) { dcs.WriteObject(ms, value); xml = Encoding.UTF8.GetString(ms.ToArray()); } return xml; } catch (Exception e) { throw; } } public static T Deserialize<T>(this string xml) where T : class { if (string.IsNullOrEmpty(xml)) { return default(T); } try { var dcs = new DataContractSerializer(typeof (T)); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { ms.Position = 0; return dcs.ReadObject(ms) as T; } } catch (Exception e) { throw; } } result: <PARAMS xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><REQUESTTYPE>36</REQUESTTYPE><STUDENTID>0</STUDENTID><YEAR>ืชืฉืข</YEAR></PARAMS> 

How to remove xmlns: I = "http://www.w3.org/2001/XMLSchema-instance" ?? After serialization

+4
source share
1 answer

Switch to using XmlSerializer

 System.Xml.Serialization.XmlSerializer 

This will create simple XML without namespaces.

-2
source

Source: https://habr.com/ru/post/1491305/


All Articles