Xmlwriter creates XML files my code

Source: Internet
Author: User

1. Use two writer. writestartelement ("") // to achieve the sub-node effect

static void main (string [] ARGs)
{< br> string app_path = application. startuppath + "\ gouwuke. XML ";
xmlwritersettings settings = new xmlwritersettings ();
Settings. indent = true;
Settings. indentchars = ("");
using (xmlwriter writer = xmlwriter. create (app_path, settings)
{< br>

// Write a comment node.
Writer. writecomment ("this is a comment"); // comment

Writer. writestartelement ("book ");
// Writer. writeattributestring ("genre", "novel"); Attribute
// Writer. writeattributestring ("ISBN", "1-8630-014"); Attribute

Sqlconnection con = new sqlconnection (@ "Server = 172.16.10.80; database = gouwuke_chs; uid = admin; Pwd = 7418 ;");
Sqldataadapter SDA = new sqldataadapter ("select Ident, productname from gwkproduct where ident like 'piaoliang100% '", con );
Dataset DS = new dataset ();
SDA. Fill (DS );
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Writer. writestartelement ("chanpin"); // subnode Effect
Writer. writeelementstring ("ident", DS. Tables [0]. Rows [I] ["ident"]. tostring ());
Writer. writeelementstring ("productname", DS. Tables [0]. Rows [I] ["productname"]. tostring ());
Writer. writeendelement ();
}
Writer. writeenddocument ();
Writer. Flush ();
Writer. Close ();

}

}

 

 

 

2. Static void main (string [] ARGs)
{
String app_path = application. startuppath + "\ gouwuke. xml ";

Xmlwritersettings settings = new xmlwritersettings ();
Settings. indent = true;
Settings. indentchars = ("");
Using (xmlwriter writer = xmlwriter. Create (app_path, settings ))
{
// Write XML data.
String pitext = "type = \" text/XSL \ "href = \" book. XSL \"";
Writer. writeprocessinginstruction ("XML-stylesheet", pitext );

// Write the documenttype node.
Writer. writedoctype ("book", null, null, "<! Entity H \ "hardcover \"> ");

// Write a comment node.
Writer. writecomment ("sample XML"); // comment

// Write the root element.
Writer. writestartelement ("book ");

// Write the genre attribute.
Writer. writeattributestring ("genre", "novel ");

// Write the ISBN attribute.
Writer. writeattributestring ("ISBN", "1-8630-014 ");

// Write the title.
Writer. writeelementstring ("title", "The Handmaid's Tale ");

// Write the style element.
Writer. writestartelement ("style ");
Writer. writeentityref ("H ");
Writer. writeendelement ();

// Write the price.
Writer. writeelementstring ("price", "19.95 ");

// Write CDATA.
Writer. writecdata ("prices 15% off !! "); // <! [CDATA [prices 15% off!]> Style

// Write the close tag for the root element.
Writer. writeendelement ();

Writer. writeenddocument ();

// Write the XML to file and close the writer.
Writer. Flush ();
Writer. Close ();

}

}

 

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.