제목 : XML 컨트롤을 사용한 회사소개 페이지 작성 예제(XML 파일 읽기 및 쓰기)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
// 파일이 있는지 없는지 검사하여 없다면 생성
if (!File.Exists(Server.MapPath("Company.xml")))
{
XmlDocument objXmlDocument = new XmlDocument();
XmlNode root = objXmlDocument.CreateElement(String.Empty, "Company", String.Empty);
objXmlDocument.AppendChild(objXmlDocument.CreateXmlDeclaration("1.0", "utf-8", "yes"));
objXmlDocument.AppendChild(root);
objXmlDocument.Save(Server.MapPath("Company.xml"));
}
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"));
XmlNode company = doc.CreateElement(String.Empty, "Company", String.Empty);
XmlNode companyName = doc.CreateElement(String.Empty, "CompanyName", String.Empty);
companyName.InnerXml = this.TextBox1.Text;
company.AppendChild(companyName);
XmlNode companyNumber = doc.CreateElement(String.Empty, "CompanyNumber", String.Empty);
companyNumber.InnerXml = this.TextBox2.Text;
company.AppendChild(companyNumber);
XmlNode companyAddress = doc.CreateElement(String.Empty, "CompanyAddress", String.Empty);
companyAddress.InnerXml = this.TextBox3.Text;
company.AppendChild(companyAddress);
XmlNode companyAddressDetail = doc.CreateElement(String.Empty, "CompanyAddressDetail", string.Empty);
companyAddressDetail.InnerXml = this.TextBox4.Text;
company.AppendChild(companyAddressDetail);
XmlNode name = doc.CreateElement(String.Empty, "Name", String.Empty);
name.InnerXml = this.TextBox5.Text;
company.AppendChild(name);
XmlNode companyType = doc.CreateElement(String.Empty, "CompanyType", String.Empty);
companyType.InnerXml = this.TextBox6.Text;
company.AppendChild(companyType);
XmlNode companyStyle = doc.CreateElement(String.Empty, "CompanyStyle", String.Empty);
companyStyle.InnerXml = this.TextBox7.Text;
company.AppendChild(companyStyle);
XmlNode varOperator = doc.CreateElement(String.Empty, "Operator", String.Empty);
varOperator.InnerXml = this.TextBox8.Text;
company.AppendChild(varOperator);
XmlNode phone = doc.CreateElement(String.Empty, "Phone", String.Empty);
phone.InnerXml = this.TextBox9.Text;
company.AppendChild(phone);
XmlNode fax = doc.CreateElement(String.Empty, "Fax", String.Empty);
fax.InnerXml = this.TextBox10.Text;
company.AppendChild(fax);
XmlNode email = doc.CreateElement(String.Empty, "Email", String.Empty);
email.InnerXml = this.TextBox11.Text;
company.AppendChild(email);
doc.AppendChild(company);
doc.Save(Server.MapPath("Company.xml"));
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (File.Exists(Server.MapPath("Company.xml")))
{
XmlDocument objXmlDocumentReader = new XmlDocument();
objXmlDocumentReader.Load(Server.MapPath("Company.xml"));
if (objXmlDocumentReader.ChildNodes.Count > 0)
{
XmlNodeList companyList = objXmlDocumentReader.SelectSingleNode("Company").ChildNodes;
this.TextBox1.Text = companyList[0].InnerXml;
this.TextBox2.Text = companyList[1].InnerXml;
this.TextBox3.Text = companyList[2].InnerXml;
this.TextBox4.Text = companyList[3].InnerXml;
this.TextBox5.Text = companyList[4].InnerXml;
this.TextBox6.Text = companyList[5].InnerXml;
this.TextBox7.Text = companyList[6].InnerXml;
this.TextBox8.Text = companyList[7].InnerXml;
this.TextBox9.Text = companyList[8].InnerXml;
this.TextBox10.Text = companyList[9].InnerXml;
this.TextBox11.Text = companyList[10].InnerXml;
}
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>간단한 XML 파일 읽기 및 쓰기 연습</title>
</head>
<body>
<form id="form1" runat="server">
<div>
사업자명:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
사업자번호:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
사업장소재지<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
사업장소재지상세<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
대표자 성명<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
업태<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />
업종(종목)<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox><br />
운영자 성명<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox><br />
연락처<asp:TextBox ID="TextBox9" runat="server"></asp:TextBox><br />
팩스<asp:TextBox ID="TextBox10" runat="server"></asp:TextBox><br />
이메일주소<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>