Method 1: use dataset directly
Sqlconnection conn = new sqlconnection (); <br/> Conn. connectionstring = "Server = 127.0.0.1; user id = sa; Password = sa; database = northwind; persist Security info = true"; <br/> Conn. open (); <br/> sqldataadapter da = new sqldataadapter ("select * from table", Conn); <br/> sqlcommandbuilder thisbulder = new sqlcommandbuilder (DA ); <br/> dataset DS = new dataset (); <br/> da. fill (DS); <br/> DS. writexml (@ "C:/temp. XML ");
Method 2: Custom generation mode
using system. XML; // Add this namespace to the header <br/> xmldocument XD = new xmldocument (); // indicates the XML document <br/> xmldeclaration xde; // indicates the XML declaration node: <? XML version = '1. 0'...?> <Br/> xde = XD. createxmldeclaration ("1.0", "GBK", null ); // The second item of the parameter is the encoding method <br/> // standalone defines whether the document can be processed without reading any other files, default Value: No <br/> XD. appendchild (xde); // <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> Generation ended <br/> xmlelement Xe = XD. createelement ("root"); // create a root element <br/> XD. appendchild (xe); // root element created <br/> xmlnode root = XD. selectsinglenode ("root"); // find <root> <br/> xmlelement xe1 = XD. createelement ("Tree"); // create an element under <root> <tree> <br/> xe1.setattribute ("ID", "1 "); // specify the property value <br/> xe1.innertext = "Type 1"; // specify the property text node <br/> root. appendchild (xe1); // subnode completion <tree> <br/> XD. save (server. mappath ("XML. XML ");