asp.net webapi 序列化為xml 時實體屬性增加防止特殊字元
阿新 • • 發佈:2019-02-01
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 namespaceMvcApplication1.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 }