How to make the records in a database into XML in C #

Source: Internet
Author: User
Tags foreach datetime split tostring trim xsl xsl file
xml| Data | The database used to generate the corresponding XML file in a company project using the records in the database [primarily to improve access speed], but because of the lack of information at that time, encountered in the development process but the difficulties, fortunately, finally completed the work, I am here at that time one of the functional functions listed, It is in the function of Datong small meaning, I hope that the brothers in the future in the face of this problem is not like I did the same.
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;

Namespace Admin
{
<summary>
Summary description of the createxml.
</summary>
///
[System.Web.Services.WebService (namespace= "Http://..../admin/createxml.asmx", description= "XML file in the build or more Nova Fan club")]

public class CreateXML:System.Web.Services.WebService
{
Public Createxml ()
{
CodeGen: This call is required by the ASP.net Web service designer
InitializeComponent ();
}

Code generated #region Component Designer

Required by the WEB service designer
Private IContainer components = null;

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
}

<summary>
Clean up all resources that are in use.
</summary>
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 ())//generation of corresponding time-type folders by period
String Filename=server.mappath (".. \ "+filenamedate." Year.tostring () + "\" +filenamedate. Month.tostring () + "_" +filenamedate. Day.tostring () + "\\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.formatting = formatting.indented;
Picxmlwriter.indentation= 6;
Picxmlwriter.namespaces = false;
Picxmlwriter.writestartdocument ();
Picxmlwriter.writedoctype ("document Type", NULL, ". xml", NULL);
Picxmlwriter.writecomment ("Read and write by ID recorded in database");
Picxmlwriter.writeprocessinginstruction ("Xml-stylesheet", "type= ' text/xsl ' href= ')." /.. /xsl/1.xsl ' "); Write the XSL file name for interpretation
Picxmlwriter.writestartelement ("", "Club", "");
foreach (DataRow R in Mydataset.tables[0]. rows)//To remove all rows sequentially
{
Picxmlwriter.writestartelement ("", "Record", "");
foreach (DataColumn c in mydataset.tables[0]. Columns)//Find all column properties of the current record in turn
{
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.writestartelement ("", c.caption.tostring (). Trim (), "" ");
string [] pic=r[c]. ToString (). Trim (). Split (' | ');
for (int i=0;i<pic. length;i++)
{

if (pic[i). Trim ()!= "")//Database picture fields are inserted in the format: file name, height, Width | Analogy. such as no.jpg,132,142|
{
Picxmlwriter.writestartelement ("", c.caption.tostring (). Trim () + "s", "");
string [] picstr=pic[i]. Split (', ');
Picxmlwriter.writestartelement ("", "Picstr", "");
Picxmlwriter.writestring (Picstr[0]. Trim (). Trim ());
Picxmlwriter.writeendelement ();

Picxmlwriter.writestartelement ("", "height", "");
Picxmlwriter.writestring (Picstr[1]. Trim (). Trim ());
Picxmlwriter.writeendelement ();

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.writeendelement ();

}
}
Picxmlwriter.writeendelement ();
}

Picxmlwriter.writeendelement ();
Picxmlwriter.flush ();

}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.tostring ());
}

Finally
{
Console.WriteLine ();
Console.WriteLine ("Processing of the file {0} is complete.) ", ID);
if (picxmlwriter!= null)
Picxmlwriter.close ();
Close writer
if (picxmlwriter!= null)
Picxmlwriter.close ();
}
Return filenamedate. Year.tostring () + "\" +filenamedate. Month.tostring () + "_" +filenamedate. Day.tostring () + "\\Club" +id. Trim () + ". xml";
}


public void Createpath (String yearpath,string monthdaycurrent)
{
String Path=server.mappath ("");
if (directory.exists (Path+yearpath))
{
if (directory.exists (path+yearpath+monthdaycurrent))
{
;
}
Else
{
Directory.CreateDirectory (path+ "\" +yearpath+ "\" +monthdaycurrent);
}
}
Else
{
Directory.CreateDirectory (path+ "\" +yearpath+ "\" +monthdaycurrent);
}

}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.