4. HtmlUtility 클래스는 HTML 태그를 특수 기호로 변경해준다. 이 클래스의 내용은 게시판의 상세보기(Details) 페이지에서 태그를 실행하지 않고 순수 텍스트로 변환해서 보여줄 때 사용된다.

    ▼  Dul 프로젝트의 HtmlUtility.cs 클래스

    using System;
    
    namespace Dul
    {
      public class HtmlUtility
      {
          #region Encode() 함수
          /// <summary>
          /// HTML을 실행하지 않고 소스 그대로 표현해서 바로 웹 페이지에 보여줌
          /// </summary>
          /// <param name=“strContent”>HTML 태그가 포함된 문자열</param>
          /// <returns>태그가 인코드되어 소스 그대로 표현될 문자열</returns>
          public static string Encode(string strContent)
          {
              string strTemp = ””;
              if (String.IsNullOrEmpty(strContent))
              {
                  strTemp = ””;
              }
              else
              {
                  strTemp = strContent;
                  strTemp = strTemp.Replace(“&“, “&amp;”);
                  strTemp = strTemp.Replace(“>“, “&gt;”);
                  strTemp = strTemp.Replace(“<“, “&lt;”);
                  strTemp = strTemp.Replace(“\r\n”, “<br />“);
                  strTemp = strTemp.Replace(“\“”, “&#34;”);
              }
              return strTemp;
          }
          #endregion
          
          #region EncodeWithTabAndSpace() 함수
          /// <summary>
          /// HTML을 실행하지 않고 소스 그대로 표현해서 바로 웹 페이지에 보여줌
          /// 추가적으로 탭과 공백도 HTML 코드(&nbsp;)로 처리해서 출력
          /// </summary>
          /// <param name=“strContent”>HTML 태그가 포함된 문자열</param>
          /// <returns>태그가 인코드되어 소스 그대로 표현될 문자열</returns>
          public static string EncodeWithTabAndSpace(string strContent)
          {
              return Encode(strContent)
                  .Replace(“\t”, “&nbsp;&nbsp;&nbsp;&nbsp;”)
                  .Replace(” “ + ” “, “&nbsp;&nbsp;”);
          }
          #endregion
      }
    }
    

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.