그냥 설명은 무시하고 예제 소스로 그냥 진행합니다.

DataSet이 아닌 일반 DataTable을 이용하는 방법도 동일합니다.

 

DataSet 생성

DataSet ds = new DataSet();
DataRow dr = null;
DataTable dt01 = new DataTable();
dt01.TableName = "Test01";
dt01.Columns.AddRange(new DataColumn[] {
        new DataColumn { ColumnName ="no", Caption = "일련번호", DataType=typeof(int), DefaultValue = 0}
        , new DataColumn { ColumnName ="col", Caption = "컬럼명", DataType=typeof(string), DefaultValue = string.Empty}
        , new DataColumn { ColumnName ="val", Caption = "값", DataType=typeof(string), DefaultValue = string.Empty}
        , new DataColumn { ColumnName ="etc", Caption = "비고", DataType=typeof(string), DefaultValue = string.Empty}
    });
dr = dt01.NewRow();
dr["no"] = 1;
dr["col"] = "COL1";
dr["val"] = "A";
dr["etc"] = "비고1";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 2;
dr["col"] = "COL2";
dr["val"] = "B";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 3;
dr["col"] = "COL3";
dr["val"] = "C";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 4;
dr["col"] = "COL4";
dr["val"] = "D";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 5;
dr["col"] = "COL5";
dr["val"] = "가";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 6;
dr["col"] = "COL6";
dr["val"] = "나※";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 7;
dr["col"] = "COL7";
dr["val"] = "~!@#$%^&*()_+`-={}[]:\";'<>?,./|\\";
dt01.Rows.Add(dr);
dr = dt01.NewRow();
dr["no"] = 8;
dr["col"] = "COL8";

dt01.Rows.Add(dr);
dr = dt01.NewRow();
dt01.Rows.Add(dr);
 
 
DataTable dt02 = new DataTable();
dt02.TableName = "Test02";
dt02.Columns.AddRange(new DataColumn[] {
        new DataColumn { ColumnName ="no", Caption = "일련번호", DataType=typeof(int), AutoIncrement = true}
        , new DataColumn { ColumnName ="etc", Caption = "비고", DataType=typeof(string), DefaultValue = string.Empty}
    });
dr = dt02.NewRow();
dr["etc"] = "비고1";
dt02.Rows.Add(dr);
dr = dt02.NewRow();
dr["etc"] = "비고2";
dt02.Rows.Add(dr);
dr = dt02.NewRow();
dr["etc"] = "비고3";
dt02.Rows.Add(dr);
dr = dt02.NewRow();
dr["etc"] = "비고4";
dt02.Rows.Add(dr);
dr = dt02.NewRow();
dr["etc"] = "비고5";
dt02.Rows.Add(dr);
dr = dt02.NewRow();
dr["etc"] = "비고6";
dt02.Rows.Add(dr);
 
ds.Tables.Add(dt01);
ds.Tables.Add(dt02);

XML 파일로 저장

ds.WriteXmlSchema(@"c:\TestForDS.xsd");
ds.WriteXml(@"c:\TestForDS.xml");

 

XML Stream을 이용하여 String에 담기

System.IO.MemoryStream mStreamXSD = new System.IO.MemoryStream();
System.IO.MemoryStream mStreamXML = new System.IO.MemoryStream();
ds.WriteXmlSchema(mStreamXSD);
mStreamXSD.Seek(0, System.IO.SeekOrigin.Begin);
ds.WriteXml(mStreamXML);
mStreamXML.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.StreamReader srXSD = new System.IO.StreamReader(mStreamXSD);
System.IO.StreamReader srXML = new System.IO.StreamReader(mStreamXML);
string strXSD = srXSD.ReadToEnd();
string strXML = srXML.ReadToEnd();

 

이 방법을 이용하여 Oracle Clob 저장 및 활용

 

=================================================================

참고(출력 파일)

XSD

<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Test01">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="no" msdata:Caption="일련번호" type="xs:int" default="0" minOccurs="0" />
              <xs:element name="col" msdata:Caption="컬럼명" type="xs:string" default="" minOccurs="0" />
              <xs:element name="val" msdata:Caption="값" type="xs:string" default="" minOccurs="0" />
              <xs:element name="etc" msdata:Caption="비고" type="xs:string" default="" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Test02">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="no" msdata:AutoIncrement="true" msdata:Caption="일련번호" type="xs:int" minOccurs="0" />
              <xs:element name="etc" msdata:Caption="비고" type="xs:string" default="" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Test01>
    <no>1</no>
    <col>COL1</col>
    <val>A</val>
    <etc>비고1</etc>
  </Test01>
  <Test01>
    <no>2</no>
    <col>COL2</col>
    <val>B</val>
    <etc />
  </Test01>
  <Test01>
    <no>3</no>
    <col>COL3</col>
    <val>C</val>
    <etc />
  </Test01>
  <Test01>
    <no>4</no>
    <col>COL4</col>
    <val>D</val>
    <etc />
  </Test01>
  <Test01>
    <no>5</no>
    <col>COL5</col>
    <val>가</val>
    <etc />
  </Test01>
  <Test01>
    <no>6</no>
    <col>COL6</col>
    <val>나※</val>
    <etc />
  </Test01>
  <Test01>
    <no>7</no>
    <col>COL7</col>
    <val>~!@#$%^&amp;*()_+`-={}[]:";'&lt;&gt;?,./|\</val>
    <etc />
  </Test01>
  <Test01>
    <no>8</no>
    <col>COL8</col>
    <val />
    <etc />
  </Test01>
  <Test01>
    <no>0</no>
    <col />
    <val />
    <etc />
  </Test01>
  <Test02>
    <no>0</no>
    <etc>비고1</etc>
  </Test02>
  <Test02>
    <no>1</no>
    <etc>비고2</etc>
  </Test02>
  <Test02>
    <no>2</no>
    <etc>비고3</etc>
  </Test02>
  <Test02>
    <no>3</no>
    <etc>비고4</etc>
  </Test02>
  <Test02>
    <no>4</no>
    <etc>비고5</etc>
  </Test02>
  <Test02>
    <no>5</no>
    <etc>비고6</etc>
  </Test02>
</NewDataSet>

출처 : 자작(userpark)

+ Recent posts