![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
查询数据库所有表名并插入XML #region 把查出来的表存起来 try { conn.Open(); SqlCommand comm = new SqlCommand("select name from sysobjects where type='U'", conn); dr = comm.ExecuteReader(); for (int i = 0; i < j; i++) { while (dr.Read()) { j++; WriteLog(dr[i].ToString()); } } } catch (Exception) { throw; } finally { if (conn.State != ConnectionState.Closed) conn.Close(); } #endregion public void WriteLog(string name) { XmlDocument XD = new XmlDocument(); XD.Load(Server.MapPath("~/Temp.xml")); XmlElement TempMessageNode = XD.CreateElement("TempMessage"); XmlNode XN = XD.SelectSingleNode("Temp"); //注释掉的部分是自动编号 //int id = 0; //if (XN.HasChildNodes) //{ // id = Convert.ToInt16(XN.LastChild.Attributes["ID"].Value) + 1; //} //else //{ // id = 1; //} //TempMessageNode.SetAttribute("ID", id.ToString()); XD.DocumentElement.PrependChild(TempMessageNode); XmlElement NameNode = XD.CreateElement("Name"); XmlText NameText = XD.CreateTextNode(name); TempMessageNode.AppendChild(NameNode); NameNode.AppendChild(NameText); XD.Save(Server.MapPath("~/Temp.xml")); }