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 ();
}
}