你的位置:首页 > 软件开发 > ASP.net > asp.net webapi 序列化为xml 时实体属性增加![CDATA[]]已防止特殊字符

asp.net webapi 序列化为xml 时实体属性增加![CDATA[]]已防止特殊字符

发布时间:2016-08-31 12:00:03
有时webapi在序列化 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Net; 5 using System.Net.Http; 6 using ...

     有时webapi在序列化

 

 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Net; 5 using System.Net.Http; 6 using System.Net.Http.Formatting; 7 using System.Threading.Tasks; 8 using System.Web.Http; 9 using System. 10 using System. 11  12 namespace MvcApplication1.Controllers 13 { 14   public class TestController : ApiController 15   { 16     [HttpGet] 17     [HttpPost] 18     public HttpResponseMessage HouseTest(get='_blank'>string city) 19     { 20       //手动构造数据,这里应该是调用构造数据。 21       var info = new GetHouseCountInfo() 22       { 23         CityName = "北京", 24         CountInfo = new List<CountInfo>() 25         { 26           new CountInfo() 27           { 28             Data = "2016-08-30", 29             HouseDetail = "描述信息1111等。。。" 30           }, 31           new CountInfo() 32           { 33             Data = "2016-08-30", 34             HouseDetail = "描述信息2222等。。。" 35           }, 36           new CountInfo() 37           { 38             Data = "2016-08-30", 39             HouseDetail = "描述信息333等。。。" 40           } 41         } 42       }; 43       //序列化实体与赋值 44       var model = new HouseCountRoot {GetHouseInfo = new GetHouseCountInfo()}; 45       model.GetHouseInfo.CountInfo = info.CountInfo; 46       model.Result = ""; 47       model.Message = ""; 48       model.GetHouseInfo.CityName = info.CityName; 49  50       return new HttpResponseMessage() 51       { 52         Content = 53           new ObjectContent<HouseCountRoot>(model, new CustomNamespacetrue}, 54             new System.Net.Http.Headers.MediaTypeHeaderValue("application/") {CharSet = "utf-8"}), 55         StatusCode = HttpStatusCode.OK 56       }; 57     } 58   } 59  60   ["houses")] 61   public class HouseCountRoot 62   { 63     ["result")] 64     public string Result { get; set; } 65  66     ["message")] 67     public string Message { get; set; } 68  69     ["housecount")] 70     public GetHouseCountInfo GetHouseInfo { get; set; } 71   } 72  73   public class GetHouseCountInfo 74   { 75     /// <summary> 76     /// 城市名称 77     /// </summary> 78     ["cityname")] 79     public string CityName { get; set; } 80  81     /// <summary> 82     /// 房源数信息 83     /// </summary> 84     ["countinfo")] 85     public List<CountInfo> CountInfo { get; set; } 86   } 87  88   public class CountInfo 89   { 90     /// <summary> 91     /// 日期 92     /// </summary> 93     ["data")] 94     public string Data { get; set; } 95  96     /// <summary> 97     /// 加<![CDATA[ ]]>数据字段 98     /// </summary> 99     [//方式1,这里属性设置忽略,把CDataContent设置为housedetail100     public string HouseDetail { get; set; }101 102     ["housedetail")]103     public 104     {105       get106       {107         return new 108         {109           new 110         };111       }112       set113       {114         HouseDetail =115           value[0].Value;116       }117     }118 119     //方式二,这里把CDataContent设置为housedetail120     //[121     //public 122     //{123     //  get124     //  {125     //    // 这种方式这里代码比上面的要多运行一定次数。126     //    127     //    node.InnerText = HouseDetail;128     //    return node;129     //  }130     //  set131     //  {132     //    HouseDetail133     //      = value.Value;134     //  } //省略则CDataContent不会被序列化135     //}136 137     //以下属性省略。。。。138   }139 140   /// <summary>141   /// 去除142   /// </summary>143   public class CustomNamespace144   {145     public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,146       TransportContext transportContext)147     {148       var xns = new 149       foreach (var attribute in type.GetCustomAttributes(true))150       {151         var as 152         if (null)153         {154           xns.Add(string.Empty, 155         }156       }157 158       if (xns.Count == 0)159       {160         xns.Add(string.Empty, string.Empty);161       }162 163       var task = Task.Factory.StartNew(() =>164       {165         var serializer = new 166         serializer.Serialize(writeStream, value, xns);167       });168 169       return task;170     }171   }172 }

原标题:asp.net webapi 序列化为xml 时实体属性增加![CDATA[]]已防止特殊字符

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。