[C #] dataset export XML (XSLT) source code

Source: Internet
Author: User

 

1. Use streamwriter to implement

Export XML # region export XML
Private bool exportxml1 (string filepath)
...{
Dataset DS = new dataset ();
Try
...{
Datatable TB = (datatable) This. exportgrid. datasource;
Datatable exporttb = Tb. Copy ();
// Datatable exporttb = new datatable ();

DS. Tables. Add (exporttb );
String Path = constant. applicationdirectory + "/XML/xyh. XSLT ";
String S = "<? XML version = "1.0" encoding = "UTF-8"?> "+" "+" <? XML-stylesheet href = "" + path + "" _ fcksavedurl = "+ path +" type = "text/XSL"?> "+ "";
// String S = "<? XML version = "1.0" encoding = "UTF-8"?> "+" "+" <? XML-stylesheet href = "AA. XSL" type = "text/XSL"?> "+ "";
S = S + Ds. getxml (). tostring ();
Stream stream = file. openwrite (filepath + ". xml ");
Using (streamwriter writer = new streamwriter (Stream ))
...{
Writer. Write (s );
}

// Ds. writexml (filepath + ". xml ");
This. parentwindow. cursor = cursors. default;
}
Catch
...{
MessageBox. Show ("An error occurred while saving. Please check! ");
Return false;
}
Finally
...{
// GC. Collect ();
// Ds. Clear ();
// Ds. Dispose ();
}
Return true;
}
# Endregion

2. Use xmltextwriter to format fields.

Export XML # region export XML
Private void exportxml (string filepath)
...{
String Path = constant. applicationdirectory + "/XML/xyh. XSLT ";
String pitext = "type = 'text/XSL 'href ='" + path + "'";
Dataset DS = new dataset ();
Datatable TB = (datatable) This. exportgrid. datasource;
Datatable exporttb = Tb. Copy ();
DS. Tables. Add (exporttb );
Xmltextreader xtreader = new xmltextreader (Ds. getxml (), xmlnodetype. element, null );
Xmltextwriter xtwriter = new xmltextwriter (filepath + ". xml", encoding. utf8 );
Xtwriter. writestartdocument ();
Xtwriter. writeprocessinginstruction ("XML-stylesheet", pitext );
String fieldname = "";
While (xtreader. Read ())
...{
Switch (xtreader. nodetype)
...{
Case xmlnodetype. element:
Xtwriter. writestartelement (xtreader. Name );
Fieldname = xtreader. Name;
Break;
Case xmlnodetype. Text:
If (fieldname. tolower () = "brithday" | fieldname. tolower () = "gzsj" | fieldname. tolower () = "rdsj" | fieldname. tolower () = "zzsj ")
...{
Datetime dt = datetime. parse (xtreader. value. tostring ());
Xtwriter. writestring (Dt. tostring ("yyyy-mm-dd "));
}
Else
Xtwriter. writestring (xtreader. value );
Break;
Case xmlnodetype. endelement:
Xtwriter. writeendelement ();
Break;
Default:
Break;
}
}
Xtwriter. Close ();
This. parentwindow. cursor = cursors. default;
}
# Endregion

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.