读取多个table,存放到dataset,创建关系,导出树形XML格式

前端之家收集整理的这篇文章主要介绍了读取多个table,存放到dataset,创建关系,导出树形XML格式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


方法一:

private void ExitXML()

{


DataSet ds = new DataSet("request");


DataTable stu = new DataTable();

stu =NewsDAL.dbServer.getTableValue("stu").Tables[0];

stu.TableName = "stu";

ds.Tables.Add(stu.Copy());



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());



DataTable Message = new DataTable();

Message = NewsDAL.dbServer.getTableValue("Message").Tables[0];

Message.TableName = "Message";

ds.Tables.Add(Message.Copy());



DataTable Course = new DataTable();

Course = NewsDAL.dbServer.getTableValue("Course").Tables[0];

Course.TableName = "Course";

ds.Tables.Add(Course.Copy());




// 建立父子关系

ds.Relations.Add("stu_Stu_Course",ds.Tables["stu"].Columns["sid"],

ds.Tables["Stu_Course"].Columns["sid"]).Nested = true;


ds.Relations.Add("stu_Message",

ds.Tables["Message"].Columns["sid"]);


XmlDocument xmldoc = new XmlDocument();

// 创建文档声明和根节点

xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0","UTF-8",null));

XmlElement xmlelem = xmldoc.CreateElement("","Request","");

xmldoc.AppendChild(xmlelem);



XmlElement xmlelem_Stus = xmldoc.CreateElement(ds.Tables[0].TableName+"s");

xmlelem.AppendChild(xmlelem_Stus);


foreach(DataRow dr in ds.Tables[0].Rows ){

XmlElement xmlelem_Stu = xmldoc.CreateElement(ds.Tables[0].TableName);

xmlelem_Stus.AppendChild(xmlelem_Stu);


foreach(DataColumn dc in ds.Tables[0].Columns ){

XmlElement xmlelem_dr = xmldoc.CreateElement(dc.ColumnName);


xmlelem_Stu.AppendChild(xmlelem_dr);

xmlelem_dr.InnerText = dr[dc.ColumnName].ToString();

}


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);


xmlelem_Stu_Course.AppendChild(xmlelem_dr2);

xmlelem_dr2.InnerText = dr2[dc2.ColumnName].ToString();

}

}


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);


xmlelem_stu_Message.AppendChild(xmlelem_dr3);

xmlelem_dr3.InnerText = dr3[dc3.ColumnName].ToString();

}

}

}


try

{

xmldoc.Save("e:\\c1.xml");

}

catch (Exception e)

{


Console.WriteLine(e.Message);

}


Console.Read();

}



方法二:

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");


}

catch (sqlException e)

{

Console.Write(e.ToString());

}

finally

{

con.Dispose();

}


// 建立父子关系

ds.Relations.Add("User_Log",ds.Tables["Users"].Columns["UserID"],

ds.Tables["Logs"].Columns["UserID"]).Nested = true;


ds.Relations.Add("Log_Tank",ds.Tables["Logs"].Columns["LogID"],

ds.Tables["Tanks"].Columns["LogID"]);


XmlDocument xmldoc = new XmlDocument();

// 创建文档声明和根节点

xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0","");

xmldoc.AppendChild(xmlelem);


// Users

XmlElement xmlelem_Users = xmldoc.CreateElement("Users");

xmlelem.AppendChild(xmlelem_Users);


foreach (DataRow patentrow in ds.Tables["Users"].Rows)

{

// User

XmlElement xmlelem_User = xmldoc.CreateElement("User");

xmlelem_Users.AppendChild(xmlelem_User);


// 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);


// 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);


// Logs

XmlElement xmlelem_Logs = xmldoc.CreateElement("Logs");

xmlelem_User.AppendChild(xmlelem_Logs);


foreach (DataRow childrow in patentrow.GetChildRows("User_Log"))

{

// Log

XmlElement xmlelem_Log = xmldoc.CreateElement("Log");

xmlelem_Logs.AppendChild(xmlelem_Log);


// 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);


// 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);


// Tanks

XmlElement xmlelem_Tanks = xmldoc.CreateElement("Tanks");

xmlelem_Log.AppendChild(xmlelem_Tanks);


foreach (DataRow childrow2 in childrow.GetChildRows("Log_Tank"))

{

// Tank

XmlElement xmlelem_Tank = xmldoc.CreateElement("Tank");

xmlelem_Tanks.AppendChild(xmlelem_Tank);


// 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);


// 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);

}


//Console.WriteLine("\t" + childrow["loginID"] + childrow["Title"] + childrow["Reason"]);

}

}


try

{

xmldoc.Save("ccc.xml");

}

catch (Exception e)

{


Console.WriteLine(e.Message);

}


Console.Read();

}

猜你在找的XML相关文章