日期:2014-05-18 浏览次数:20732 次
OleDbData md = new OleDbData(); md.ExecuteReader("select * from tbDept"); XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement("root"); xmlDoc.AppendChild(root); while (md.Reader.Read()) { int parentID = Convert.ToInt32(md.Reader["ParentID"]); XmlElement node = xmlDoc.CreateElement("node"); for (int i = 0; i < md.Reader.FieldCount; i++) node.SetAttribute(md.Reader.GetName(i), md.Reader[i].ToString()); if (parentID > 0) { xmlDoc.SelectSingleNode(string.Format(@"//node[@ID='{0}']", md.Reader["ParentID"])).AppendChild(node); } else { xmlDoc.DocumentElement.AppendChild(node); } } md.Close(); xmlDoc.Save(Server.MapPath("~/test.xml"));