[ ASP.NET ] XML 출력 / XML 형태로 출력하는 방법 / XML생성 / RESPONSE.CONTENTTYPE


URL로 요청을 받아서 xml 형태로 뿌려줘야 할 때가 종종 있는데, 

간단하게 아래와 같이 세팅해주면 웹 브라우져에서 xml형태로 출력되는 것을 알 수 있다.







-> cs


1
2
3
4
5
6
7
8
9
10
11
12
13
//xmlDoc선언.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0""utf-8""yes"));
 
//xmlDoc에 출력할 내용을 더함.
XmlElement goods = xmlDoc.CreateElement("GOOD_ARRAY");
goods.InnerText = "test"
xmlDoc.DocumentElement.AppendChild(goods);
 
//xml 형태로 화면에 출력.
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";
xmlDoc.Save(Response.Output);
cs




-> aspx


1
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" ContentType="text/xml" %>
cs


+ Recent posts