2013-07-14 154 views
4

我有兩個簡單的序列化/ desirialize方法,刪除命名空間不起作用

映射:

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

如何刪除的xmlns:I =「HTTP://www.w3。 org/2001/XMLSchema-instance「?在序列化

回答

-2

切換到使用XmlSerializer的

System.Xml.Serialization.XmlSerializer 

這將產生沒有命名空間的純XML