有時webapi在序列化xml時,可能需要給某些帶有html或特殊字符(如 < > & /)的字段加上<![CDATA[]]> 已防止影響xml正常數據,如果使用.aspx視圖那可直接在前台綁定字段時直接加入<![CDATA[]]>,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.Xml; 10 using System.Xml.Serialization; 11 12 namespace MvcApplication1.Controllers 13 { 14 public class TestController : ApiController 15 { 16 [HttpGet] 17 [HttpPost] 18 public HttpResponseMessage HouseTest(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 CustomNamespaceXmlFormatter() {UseXmlSerializer = true}, 54 new System.Net.Http.Headers.MediaTypeHeaderValue("application/xml") {CharSet = "utf-8"}), 55 StatusCode = HttpStatusCode.OK 56 }; 57 } 58 } 59 60 [XmlRoot("houses")] 61 public class HouseCountRoot 62 { 63 [XmlElement("result")] 64 public string Result { get; set; } 65 66 [XmlElement("message")] 67 public string Message { get; set; } 68 69 [XmlElement("housecount")] 70 public GetHouseCountInfo GetHouseInfo { get; set; } 71 } 72 73 public class GetHouseCountInfo 74 { 75 /// <summary> 76 /// 城市名稱 77 /// </summary> 78 [XmlElement("cityname")] 79 public string CityName { get; set; } 80 81 /// <summary> 82 /// 房源數信息 83 /// </summary> 84 [XmlElement("countinfo")] 85 public List<CountInfo> CountInfo { get; set; } 86 } 87 88 public class CountInfo 89 { 90 /// <summary> 91 /// 日期 92 /// </summary> 93 [XmlElement("data")] 94 public string Data { get; set; } 95 96 /// <summary> 97 /// 加<![CDATA[ ]]>數據字段 98 /// </summary> 99 [XmlIgnore] //方式1,這里屬性設置忽略,把CDataContent設置為housedetail 100 public string HouseDetail { get; set; } 101 102 [XmlElement("housedetail")] 103 public XmlNode[] CDataContent 104 { 105 get 106 { 107 return new XmlNode[] 108 { 109 new XmlDocument().CreateCDataSection(HouseDetail) 110 }; 111 } 112 set 113 { 114 HouseDetail = 115 value[0].Value; 116 } 117 } 118 119 //方式二,這里把CDataContent設置為housedetail 120 //[XmlElement("housedetail")] 121 //public XmlNode CDataContent 122 //{ 123 // get 124 // { 125 // // 這種方式這里代碼比上面的要多運行一定次數。 126 // XmlNode node = new XmlDocument().CreateNode(XmlNodeType.CDATA, "", ""); 127 // node.InnerText = HouseDetail; 128 // return node; 129 // } 130 // set 131 // { 132 // HouseDetail 133 // = value.Value; 134 // } //省略則CDataContent不會被序列化 135 //} 136 137 //以下屬性省略。。。。 138 } 139 140 /// <summary> 141 /// 去除xml命名空間的 序列化類 142 /// </summary> 143 public class CustomNamespaceXmlFormatter : XmlMediaTypeFormatter 144 { 145 public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, 146 TransportContext transportContext) 147 { 148 var xns = new XmlSerializerNamespaces(); 149 foreach (var attribute in type.GetCustomAttributes(true)) 150 { 151 var xmlRootAttribute = attribute as XmlRootAttribute; 152 if (xmlRootAttribute != null) 153 { 154 xns.Add(string.Empty, xmlRootAttribute.Namespace); 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 XmlSerializer(type); 166 serializer.Serialize(writeStream, value, xns); 167 }); 168 169 return task; 170 } 171 } 172 }
結果如下。
1 <?xml version="1.0"?> 2 <houses> 3 <result /> 4 <message /> 5 <housecount> 6 <cityname>北京</cityname> 7 <countinfo> 8 <data>2016-08-30</data> 9 <housedetail><![CDATA[描述信息1111等。。。]]></housedetail> 10 </countinfo> 11 <countinfo> 12 <data>2016-08-30</data> 13 <housedetail><![CDATA[描述信息2222等。。。]]></housedetail> 14 </countinfo> 15 <countinfo> 16 <data>2016-08-30</data> 17 <housedetail><![CDATA[描述信息333等。。。]]></housedetail> 18 </countinfo> 19 </housecount> 20 </houses>