다음을 통해 공유


JSON 또는 XML을 클래스로 붙여넣기

Visual Studio에서는 JSON 또는 XML 파일에서 텍스트를 복사한 다음 C# 또는 Visual Basic 코드에 클래스로 붙여 넣을 수 있습니다. 이렇게 하려면Edit Paste Special(선택하여> 붙여넣기)을 선택하고 JSON을 클래스로 붙여넣기 또는 XML을 클래스로 붙여넣기를 선택합니다.

Visual Studio의 편집 메뉴에 있는 선택하여 붙여넣기 옵션의 스크린샷

팁 (조언)

편집 메뉴에 선택하여 붙여넣기 옵션이 표시되지 않으면 ASP.NET 및 웹 개발, Azure 개발 또는 .NET 데스크톱 개발 워크로드 중 하나 이상이 설치되어 있는지 확인합니다. 그런 다음 앱에 대한 프로그램 파일을 선택해야 합니다. 예를 들어 C# 앱의 경우 솔루션 탐색기에서 Program.cs 파일을 선택합니다.

JSON (J, ava,S, cript , O, bject, N, otation)과 XML (e, X, tensible, M, arkup , L, anguage)은 둘 다 데이터를 저장하고 전송하는 데 사용된다는 점에서 유사합니다. 그러나 JSON은 덜 장황하고 배열을 사용할 수 있습니다.

예시

Visual Studio에서 JSON을 클래스로 붙여넣기 명령 또는 XML을 클래스로 붙여넣기 명령을 사용하기 전에 텍스트에 대한 자리 표시자를 만듭니다. C# 앱의 경우 다음 스크린샷과 같이 빈 네임스페이스 선언을 사용하여 이 작업을 수행할 수 있습니다.

JSON 또는 XML 텍스트에 붙여넣기 위한 자리 표시자로 사용되는 Visual Studio의 빈 네임스페이스 선언 스크린샷입니다.

그런 다음 중괄호 안에 JSON 또는 XML 텍스트를 붙여넣습니다.

JSON (자바스크립트 객체 표기법)

JSON 텍스트의 예는 다음과 같습니다.

{
  "Colors": [
 {
   "numberKey": 1,
   "isPrimary": true,
   "listColors": ["Red", "Blue", "Yellow"]
 },

 {
   "numberKey": 2,
   "isPrimary": false,
   "listColors": ["Purple", "Green", "Orange"]
 } ]
}

다음은 Visual Studio에서 JSON 텍스트를 클래스로 변환하는 방법을 보여 주는 스크린샷입니다.

Visual Studio의 선택하여 붙여넣기 기능을 사용하여 클래스로 변환된 JSON 예제 텍스트의 스크린샷입니다.

XML

다음은 XML 텍스트의 예입니다.

<root>
 <color>
  <id>01</id>
  <name>red</name>
  <type>primary</type>
 </color>
 <color>
  <id>02</id>
  <name>green</name>
  <type>secondary</type>
 </color>
</root>

다음은 Visual Studio에서 XML 텍스트를 클래스로 변환하는 방법을 보여 주는 코드 예제입니다.

using System;

namespace PasteDemo
{
    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class root
    {

        private rootColor[] colorField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("color")]
        public rootColor[] color
        {
            get
            {
                return this.colorField;
            }
            set
            {
                this.colorField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class rootColor
    {

        private byte idField;

        private string nameField;

        private string typeField;

        /// <remarks/>
        public byte id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        public string type
        {
            get
            {
                return this.typeField;
            }
            set
            {
                this.typeField = value;
            }
        }
    }
}

참고하십시오