Generate a menu in an infinite-level XML format according to a data table

xiaoxiao2021-03-06  98

Data Sheet: ID Name ParentID1 file 02 Open 13 New 14 Open File 25 Open Project 26 Editing 07 New File 38 New Project 39 New Text File 710 New Program Document 7 Source Program: Using System; Using System.xml; use system.data; Using system.data.sqlclient;

Public class menu {private static sqlconnection conn = new sqlconnection ("server = walter; uid = sa; password = sa; database = test");

public static void Main (string [] args) {XmlTextWriter xw = new XmlTextWriter ( "Menu.xml", System.Text.Encoding.GetEncoding ( "GB2312")); xw.Formatting = Formatting.Indented; xw.WriteStartDocument () Xiw.WriteStartElement ("Menuitems"); conn.open (); menu m = new menu (); M.CreateMenu (xw, 0); xw.writeEndelement (); xw.writeEndDocument (); xw.close () }

public void CreateMenu (XmlTextWriter xw, int ParentID) {SqlDataAdapter cmd = new SqlDataAdapter ( "select * from Menu where ParentID =" ParentID.ToString (), conn); DataSet ds = new DataSet (); cmd.Fill (ds, "T"); Foreach (DATAROW DR IN DS.TABLES ["T"]. Rows) {xw.writestartElement ("MenuItem"); xw.writeattributeString ("text", DR [1] .tostring ()); CreateMenu (xw, int32.parse (DR [0] .tostring ())); xw.writeEndelement ();}}}

转载请注明原文地址:https://www.9cbs.com/read-123656.html

New Post(0)