예제에 쓰일 booksourcedb데이터 베이스의 book테이블 값들

 

 

 

XML 문서형식으로 만들어 보기

SqlDataAdapter adapter;
DataSet ds;
DataSet tables;
private void Form3_Load(object sender, EventArgs e)
{
string constr = "SERVER=localhost;DATABASE=

booksourcedb;UID=sa;PWD=서버암호";
this.adapter = new SqlDataAdapter("select * from book", constr);
ds = new DataSet("booklist");
this.adapter.Fill(ds, "book");

}

DataSet과 연결시킬 SqlDataAdapter 객체를 만들어 DataSet객체에 데이터 베이스의 'book'테이블 내용을 채워 넣어 초기화 시켰다.

private void GetXml_Click(object sender, EventArgs e)
{
//DataSet-->Xml
string xml = this.ds.GetXml();
Console.WriteLine(xml);
}

GetXml()함수를 사용해서 테이블의 내용을 XML문서 형태로 만들어 주었다. DataSet과 SqlDataAdapter의 개념이해는 ADO.NET에서 2번째 정리를 보면 도움이 될것 같다. 더 정확한 정보는 MSDN에 있으니 참고 하길...

 

 

 

 

결과출력

 

XSD(스키마) 형식으로 만들어 보기

private void GetXmlSchema_Click(object sender, EventArgs e)
{
string schema = this.ds.GetXmlSchema();
Console.WriteLine(schema);
}

데이터셋에있는 테이블을 토대로 스키마 문서 형태로 바꿔 주는 함수 이다.

 

 

 

 

 

 

 

위에 두 출력을 파일로 만들기

파일로 만들때는 함수 이름에 Write가 붙는다.

위에서 쓰인 객체명 그대로 예제로 쓰겠다.

this.ds.WriteXml(@"c:\book.xml");

함수의 인자로 경로를 주면 해당 경로에 해당 문서 형태대로 문서가 생긴다~

this.ds.WriteXmlSchema(@"c:\bookshema.xsd");

 

 

 

결과

 

 

 

 

 

 

 

 

 

+ Recent posts