The entity attributes increase <! when ASP. NET Webapi is serialized as XML [cdata[]]> Prevent special characters

Source: Internet
Author: User
Tags cdata

Sometimes webapi may need to add <! to some fields with HTML or special characters (such as < > &/) when serializing XML. [cdata[]]> has prevented normal data from affecting XML, if you use an. aspx view that directly joins the <! when the field is bound in the foreground [Cdata[]]>,webapi only backstage code, that can only be done in the background, as follows.

  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; Ten using System.Xml.Serialization; Namespace Mvcapplication1.controllers 15 {16 public class Testcontroller:apicontroller HttpGet] [HttpPost] httpresponsemessage housetest (string city) 19 {20// To construct the data manually, this should be called construction data.                 $ var info = new Gethousecountinfo () {cityname = "Beijing", 24                     Countinfo = new List<countinfo> () {27 New Countinfo () {Page Data = "2016-08-30", Housedetail = "description information 1111 etc. ...                   "32}, new Countinfo () {33      data = "2016-08-30", Housedetail = "description information 2222 etc. ... "+", Countinfo new () PNS A = "2016-08-30", Housedetail = "description Information 333 etc. ... "40} 41} 42}; 43//Serialized entity with assignment of var model = new Housecountroot {gethouseinfo = new Gethousecountinfo ()}; Model. Gethouseinfo.countinfo = info. Countinfo; The model. Result = ""; Model. Message = ""; Model. Gethouseinfo.cityname = info. CityName; Httpresponsemessage return new () {Content = n                         EW objectcontent

The results are as follows.

1 <?xml version= "1.0"?> 2 

The entity attributes increase <! when ASP. NET Webapi is serialized as XML [cdata[]]> Prevent special characters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.