利用c#将数据库中的数据读到XML文件中
事件处理程序:
dataConn dc = new dataConn();
DataTable dt = new DataTable();
dt = dc.DataTable( "select * from userinfo ");
XmlTextWriter xtw = new XmlTextWriter( "test12.xml ", Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.Indentation = 1;
xtw.IndentChar = Convert.ToChar( "\t ");
xtw.QuoteChar = Convert.ToChar( "\ " ");
xtw.WriteStartDocument();
xtw.WriteStartElement( "userInfo ");
for (int i = 0; i < dt.Rows.Count; i++)
{
xtw.WriteStartElement( "user ");
xtw.WriteAttributeString( "name ", dt.Rows[i][ "userName "].ToString());
xtw.WriteAttributeString( "password ", dt.Rows[i][ "userPassword "].ToString());
xtw.WriteEndElement();
}
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Close();
Application.Exit();
目标文件:
<?xml version= "1.0 " encoding= "utf-8 "?>
<userInfo>
<user name= "艾林 " password= "12517820817 " />
<user name= "艾青 " password= "12518581127 " />
<user name= "安平 " password= "12513222515 " />
<user name= "薄帆 " password= "12517344910 " />
<user name= "曹东锋 "