@H_403_1@
方法一:
private void ExitXML()
{
@H_403_1@
DataSet ds = new DataSet("request");
@H_403_1@
DataTable stu = new DataTable();
stu =NewsDAL.dbServer.getTableValue("stu").Tables[0];
stu.TableName = "stu";
ds.Tables.Add(stu.Copy());
@H_403_1@
@H_403_1@
DataTable Stu_Course = new DataTable();
Stu_Course = NewsDAL.dbServer.getTableValue("Stu_Course").Tables[0];
Stu_Course.TableName = "Stu_Course";
ds.Tables.Add(Stu_Course.Copy());
@H_403_1@
@H_403_1@
DataTable Message = new DataTable();
Message = NewsDAL.dbServer.getTableValue("Message").Tables[0];
Message.TableName = "Message";
ds.Tables.Add(Message.Copy());
@H_403_1@
@H_403_1@
DataTable Course = new DataTable();
Course = NewsDAL.dbServer.getTableValue("Course").Tables[0];
Course.TableName = "Course";
ds.Tables.Add(Course.Copy());
@H_403_1@
@H_403_1@
@H_403_1@
// 建立父子关系
ds.Relations.Add("stu_Stu_Course",ds.Tables["stu"].Columns["sid"],
ds.Tables["Stu_Course"].Columns["sid"]).Nested = true;
@H_403_1@
ds.Relations.Add("stu_Message",
ds.Tables["Message"].Columns["sid"]);
@H_403_1@
XmlDocument xmldoc = new XmlDocument();
// 创建文档声明和根节点
xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0","UTF-8",null));
XmlElement xmlelem = xmldoc.CreateElement("","Request","");
xmldoc.AppendChild(xmlelem);
@H_403_1@
@H_403_1@
XmlElement xmlelem_Stus = xmldoc.CreateElement(ds.Tables[0].TableName+"s");
xmlelem.AppendChild(xmlelem_Stus);
@H_403_1@
foreach(DataRow dr in ds.Tables[0].Rows ){
XmlElement xmlelem_Stu = xmldoc.CreateElement(ds.Tables[0].TableName);
xmlelem_Stus.AppendChild(xmlelem_Stu);
@H_403_1@
foreach(DataColumn dc in ds.Tables[0].Columns ){
XmlElement xmlelem_dr = xmldoc.CreateElement(dc.ColumnName);
@H_403_1@
xmlelem_Stu.AppendChild(xmlelem_dr);
xmlelem_dr.InnerText = dr[dc.ColumnName].ToString();
}
@H_403_1@
XmlElement xmlelem_Stu_Courses = xmldoc.CreateElement(ds.Tables[1].TableName + "s");
xmlelem_Stu.AppendChild(xmlelem_Stu_Courses);
foreach (DataRow dr2 in dr.GetChildRows("stu_Stu_Course"))
{
XmlElement xmlelem_Stu_Course = xmldoc.CreateElement(ds.Tables[1].TableName);
xmlelem_Stu_Courses.AppendChild(xmlelem_Stu_Course);
foreach (DataColumn dc2 in ds.Tables[1].Columns)
{
XmlElement xmlelem_dr2 = xmldoc.CreateElement(dc2.ColumnName);
@H_403_1@
xmlelem_Stu_Course.AppendChild(xmlelem_dr2);
xmlelem_dr2.InnerText = dr2[dc2.ColumnName].ToString();
}
}
@H_403_1@
XmlElement xmlelem_stu_Messages = xmldoc.CreateElement(ds.Tables[2].TableName + "s");
xmlelem_Stu.AppendChild(xmlelem_stu_Messages);
foreach (DataRow dr3 in dr.GetChildRows("stu_Message"))
{
XmlElement xmlelem_stu_Message = xmldoc.CreateElement(ds.Tables[2].TableName);
xmlelem_stu_Messages.AppendChild(xmlelem_stu_Message);
foreach (DataColumn dc3 in ds.Tables[2].Columns)
{
XmlElement xmlelem_dr3 = xmldoc.CreateElement(dc3.ColumnName);
@H_403_1@
xmlelem_stu_Message.AppendChild(xmlelem_dr3);
xmlelem_dr3.InnerText = dr3[dc3.ColumnName].ToString();
}
}
}
@H_403_1@
try
{
xmldoc.Save("e:\\c1.xml");
}
catch (Exception e)
{
@H_403_1@
Console.WriteLine(e.Message);
}
@H_403_1@
Console.Read();
}
@H_403_1@
@H_403_1@
方法二:
private static void CreateXmlForDataSetRelation()
{
sqlConnection con = new sqlConnection(@"server=.;user=sa;pwd=alog;database=CestDB");
sqlDataAdapter adpt = new sqlDataAdapter("SELECT * FROM Users",con);
DataSet ds = new DataSet("request");
try
{
con.Open();
adpt.Fill(ds,"Users");
adpt.SelectCommand = new sqlCommand("SELECT * FROM Logs",con);
adpt.Fill(ds,"Logs");
adpt.SelectCommand = new sqlCommand("SELECT * FROM Tanks","Tanks");
@H_403_1@
}
catch (sqlException e)
{
Console.Write(e.ToString());
}
finally
{
con.Dispose();
}
@H_403_1@
// 建立父子关系
ds.Relations.Add("User_Log",ds.Tables["Users"].Columns["UserID"],
ds.Tables["Logs"].Columns["UserID"]).Nested = true;
@H_403_1@
ds.Relations.Add("Log_Tank",ds.Tables["Logs"].Columns["LogID"],
ds.Tables["Tanks"].Columns["LogID"]);
@H_403_1@
XmlDocument xmldoc = new XmlDocument();
// 创建文档声明和根节点
xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0","");
xmldoc.AppendChild(xmlelem);
@H_403_1@
// Users
XmlElement xmlelem_Users = xmldoc.CreateElement("Users");
xmlelem.AppendChild(xmlelem_Users);
@H_403_1@
foreach (DataRow patentrow in ds.Tables["Users"].Rows)
{
// User
XmlElement xmlelem_User = xmldoc.CreateElement("User");
xmlelem_Users.AppendChild(xmlelem_User);
@H_403_1@
// UserID
XmlElement xmlelem_UserID = xmldoc.CreateElement("UserID");
XmlText xmltxt_UserID = xmldoc.CreateTextNode(patentrow["UserID"].ToString());
xmlelem_UserID.AppendChild(xmltxt_UserID);
xmlelem_User.AppendChild(xmlelem_UserID);
@H_403_1@
// UserName
XmlElement xmlelem_UserName = xmldoc.CreateElement("UserName");
XmlText xmltxt_UserName = xmldoc.CreateTextNode(patentrow["UserName"].ToString());
xmlelem_UserName.AppendChild(xmltxt_UserName);
xmlelem_User.AppendChild(xmlelem_UserName);
@H_403_1@
// Logs
XmlElement xmlelem_Logs = xmldoc.CreateElement("Logs");
xmlelem_User.AppendChild(xmlelem_Logs);
@H_403_1@
foreach (DataRow childrow in patentrow.GetChildRows("User_Log"))
{
// Log
XmlElement xmlelem_Log = xmldoc.CreateElement("Log");
xmlelem_Logs.AppendChild(xmlelem_Log);
@H_403_1@
// LogID
XmlElement xmlelem_LogID = xmldoc.CreateElement("LogID");
XmlText xmltxt_LogID = xmldoc.CreateTextNode(childrow["LogID"].ToString());
xmlelem_LogID.AppendChild(xmltxt_LogID);
xmlelem_Log.AppendChild(xmlelem_LogID);
@H_403_1@
// LogDate
XmlElement xmlelem_LogDate = xmldoc.CreateElement("LogDate");
XmlText xmltxt_LogDate = xmldoc.CreateTextNode(childrow["LogDate"].ToString());
xmlelem_LogDate.AppendChild(xmltxt_LogDate);
xmlelem_Log.AppendChild(xmlelem_LogDate);
@H_403_1@
// Tanks
XmlElement xmlelem_Tanks = xmldoc.CreateElement("Tanks");
xmlelem_Log.AppendChild(xmlelem_Tanks);
@H_403_1@
foreach (DataRow childrow2 in childrow.GetChildRows("Log_Tank"))
{
// Tank
XmlElement xmlelem_Tank = xmldoc.CreateElement("Tank");
xmlelem_Tanks.AppendChild(xmlelem_Tank);
@H_403_1@
// TankName
XmlElement xmlelem_TankName = xmldoc.CreateElement("TankName");
XmlText xmltxt_TankName = xmldoc.CreateTextNode(childrow2["TankName"].ToString());
xmlelem_TankName.AppendChild(xmltxt_TankName);
xmlelem_Tank.AppendChild(xmlelem_TankName);
@H_403_1@
// Tankscore
XmlElement xmlelem_Tankscore = xmldoc.CreateElement("Tankscore");
XmlText xmltxt_Tankscore = xmldoc.CreateTextNode(childrow2["Tankscore"].ToString());
xmlelem_Tankscore.AppendChild(xmltxt_Tankscore);
xmlelem_Tank.AppendChild(xmlelem_Tankscore);
}
@H_403_1@
//Console.WriteLine("\t" + childrow["loginID"] + childrow["Title"] + childrow["Reason"]);
}
}
@H_403_1@
try
{
xmldoc.Save("ccc.xml");
}
catch (Exception e)
{
@H_403_1@
Console.WriteLine(e.Message);
}
@H_403_1@
Console.Read();
}
原文链接:/xml/296703.html