How to make XML in the database with C #

xiaoxiao2021-03-06  43

Previously, in a company project, the records in the database generated the corresponding XML file [mainly to improve access speed], but due to the lack of data at the time, it was difficult to have in the development process, and finally completed the work. I am here, which is listed here. The function is very small, I hope that the brothers will not be like me in the same time when they encounter such problems.

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.diagnostics;

Using system.Web;

Using system.Web.services;

USING SYSTEM.XML;

Using system.data.sqlclient;

Using system.configuration;

Using system.text;

Using system.xml.xsl;

Using system.io;

A summary description of Namespace admin {////

/// CreateXML. /// /// [system.web.services.Webservice (namespace = "http: //..../admin/createxml.asmx", description = "Generate or update XML in the Star Club file")]

Public class createXML: system.web.services.webservice {public createXML () {// codegen: This call is the initializeComponent () () () necessary for the ASP.NET Web service designer.

#Region component designer generated code // Web service designer essential private icontainer components = null; ///

/// designer supports the required method - Do not use the code editor to modify /// The content of the method. /// private void initializecomponent ()}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (Disposing && Components! = NULL) {components.dispose ();} Base.dispose (Disposing);} #ENDREGION

[WebMethod] public string CreateClubXMLByID (String ID) {DateTime filenamedate = DateTime.Now; CreatePath ( "..//" filenamedate.Year.ToString (), filenamedate.Month.ToString () " _ " filenamedate.Day .Tostring ()); // Generate the corresponding period folder String filename = server.mappath ("..//" filenamedate.year.tostring () " // " filenamedate.month.tostring () ) "_" filenamedate.day.toT7tring () "// Club" id.trim () "xml"); XMLTextWriter PicxMLWriter = NULL; Encoding GB = Encoding.Getencoding ("GB2312"); PicxMLWRITER = new XmlTextWriter (filename, GB); try {string strConn = ConfigurationSettings.AppSettings [ "starclub"]; String SQLStatement = "SELECT * FROM club where id =" ID.ToString () Trim ();. SqlConnection myConnection = new SqlConnection (strConn); SqlDataAdapter myCommand = new SqlDataAdapter (SQLStatement, myConnection); DataSet myDataSet; myCommand.SelectCommand.CommandType = CommandType.Text; myDataSet = new DataSet (); myCommand.Fill (myDataSet, "MyTable"); PicXmlWriter.Format ting = Formatting.Indented; PicXmlWriter.Indentation = 6; PicXmlWriter.Namespaces = false; PicXmlWriter.WriteStartDocument (); //PicXmlWriter.WriteDocType ( "document type", null, ".xml", null); //PicXmlWriter.WriteComment ("Record read and write in the ID recorded in the database"); PicxMLWRITER.WRITEPROCESSINSTRUCTION ("XML-Stylesheet", "Type = 'text / xsl' href = '.. /. / Xsl / 1.xsl'" ); // Write the XSL file name for explanation PicxMLWRITER.WRITESTARTELEMENT ("," Club ",");

Foreach (DATAROW R IN MyDataSet.Tables [0] .ROWS) // Remove all rows {PicxMLWRITER.WRITESTARTELEMENT ("RECORD", ",", ",", " // Oblue all column properties {IF ((c.caption.tostring ()! = "Pic")) {picxmlwriter.writestartElement (", c.caption.toString (). Trim ()," "); // Write field name PicxMLWRITER.WRITESTRING (R [C] .tostring (). Trim ()); // Write data picxmlwriter.writeEndElement ();} else {picxmlwriter.writestrtlement (" ", c. CAPTION.TOSTRING (). TRIM (), ""); string [] PIC = R [C] .tostring (). Trim (). Split ('|'); for (INT i = 0; i

PicxMlwriter.writestartElement ("Width", ""; picxmlwriter.writestring (picstr [1] .trim (). Trim ()); picxmlwriter.writeEndelement (); picxmlwriter.writestartElement ("", "comment", ""); Picxmlwriter.writestring (PIC [ i] .trim (). Trim ()); picxmlwriter.writeEndelement (); picxmlwriter.writeEndelement ();} else {i ;}} picxmlwriter.writeEndeEndelement () } Picxmlwriter.writeEndelement ();} picxmlwriter.writeEndeEndelement (); picxmlwriter.flush ();} catch (exception e) {console.writeline ("exception: {0}", e.tostring ());} finally {Console .Writeline (); console.writeline ("The processing of file {0} has been completed.", Id); if (picxmlwriter! = Null) picxmlwriter.close (); // Close the writer IF (PicxMLWRITER! = Null) Picxmlwriter.close ();} return filenamedate.year.tostring () "//" filenamedate.month.tostring () "_" filenamedate.day.tostring () "// club" id.t.t RIM () ". xml";} public void createpath (String YearPath, String MonthdayCurrent) {string path = server.mappath (""); if (Directory.exists (Directory.exists) {IF (Directory.exists path yearpath monthdaycurrent)) {;} else {Directory.CreateDirectory (path "//" yearpath "//" monthdaycurrent);}} else {Directory.CreateDirectory (path "//" yearpath "//" MONTHDAYCURRENT;}

}

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

New Post(0)