XML Declaration
<? XML version = "1.0" encoding = "gb2312" standalone = "no"?>
<Tag attribute name = "attribute value"…> Data content </Tag>
Dataset Method
Dataset. readxml (string strfilename );
Dataset. writexml (string strfilename );
Dbguest. XSD
Dbguest. xml
<? XML version = "1.0" standalone = "yes"?>
<Dbguest xmlns = "http://tempuri.org/dbGuest.xsd">
<User>
<Name> gloomyboyo </Name>
<City> Fuzhou </city>
<Email> gloomyboyo@126.com </Email>
<Message> hello! </Message>
<Stime> 10:51:14 </stime>
</User>
</Dbguest>
Private void btnsubmit_click (Object sender, system. eventargs E)
{
Dataset DS = new dataset ("myds ");
// Read data from XML
DS. readxml (server. mappath (@ "./DB/dbguest. xml "));
// Add a record as follows
Datarow DR = Ds. Tables [0]. newrow ();
Dr ["name"] = tbname. Text. Trim ();
Dr ["city"] = tbcity. Text. Trim ();
Dr ["email"] = tbemail. Text. Trim ();
Dr ["message"] = tbcomments. Text. Trim ();
Dr ["stime"] = datetime. Now. tostring ();
DS. Tables [0]. Rows. Add (DR );
// Write back XML
DS. writexml (server. mappath (". // dB // dbguest. xml "));
Response. Redirect ("view. aspx ");
}
Private void page_load (Object sender, system. eventargs E)
{// Display data
Dataset DS = new dataset ();
DS. readxml (server. mappath (@ "./DB/dbguest. xml "));
Dgshow. datasource = Ds. Tables [0]. defaultview;
Dgshow. databind ();
}
XML file read/write
Foreach (xmlnode personelement in root. childnodes)
Use of xmltextwriter
People. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<People>
<Person> Zhang San </person>
<Person> Li Si </person>
</People>
Private void loadxmlbutton_click (Object sender, system. eventargs E)
{
// Create an instance of the xmldocument class
Xmldocument Doc = new xmldocument ();
Arraylist nodevalues = new arraylist ();
// Read the people. xml file into the memory to form a DOM Structure
Doc. Load (server. mappath ("People. xml "));
Xmlnode root = Doc. documentelement;
Foreach (xmlnode personelement in root. childnodes)
Nodevalues. Add (personelement. firstchild. value );
Xmlnodelistbox. datasource = nodevalues;
Xmlnodelistbox. databind ();
}
Static void writexmlbyxmlwriter (xmlwriter writer, string symbol, double price, double change, long volume)
{
Writer. writestartelement ("stock ");
Writer. writeattributestring ("symbol", symbol );
Writer. writeelementstring ("price", xmlconvert. tostring (price ));
Writer. writeelementstring ("change", xmlconvert. tostring (Change ));
Writer. writeelementstring ("volume", xmlconvert. tostring (volume ));
Writer. writeendelement ();
}
Private void createxmlfilebutton_click (Object sender, system. eventargs E)
{
// Create a string that represents the path of the XML file to be generated. If the path points to the NTFS partition, related access permissions are required.
String filename = xmlfilepathtextbox. text;
// Create a file stream for writing XML data
System. Io. filestream myfilestream = new system. Io. filestream (filename, system. Io. filemode. Create );
// Create an xmltextwriter object using a file stream object
Xmltextwriter myxmlwriter = new xmltextwriter (myfilestream, system. Text. encoding. Unicode );
Myxmlwriter. Formatting = formatting. indented;
Try
{
// Use writexmlbyxmlwriter to write data to the xmltextwriter object
Writexmlbyxmlwriter (myxmlwriter, "MSFT", 74.5, 5.5, 49020000 );
// Call the close method to write the data of the xmltextwriter object to the XML file.
Myxmlwriter. Close ();
Page. response. Write ("XML document generated successfully! ");
}
Catch
{
Page. response. Write ("An error occurred while generating the XML document! Check whether the path is correct and whether you have write permission. ");
}
}
XML file operations
Xmldocument usage:
. Load
. Selectsinglenode
. Createelement
. Save
Dbguest. xml
<? XML version = "1.0" standalone = "yes"?>
<Dbguest>
<User>
</User>
<User>
<Name> gloomyboyo </Name>
<City> Fuzhou </city>
<Email> gloomyboyo@126.com </Email>
</User>
</Dbguest>
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
BIND ();
}
Private void BIND ()
{
Dataset DS = new dataset ();
DS. readxml (server. mappath (". // dB // dbguest. xml "));
Dgshow. datasource = Ds. Tables [0]. defaultview;
Dgshow. databind ();
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath (". // dB // dbguest. xml "));
Xmlnodelist elemlist = Doc. getelementsbytagname ("name ");
Ddlname. Items. Clear ();
For (INT I = 0; I <elemlist. Count; I ++)
Ddlname. Items. Add (elemlist [I]. innerxml );
}
Private void btnquery_click (Object sender, system. eventargs E)
{// Query
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath (". // dB // dbguest. xml "));
Lbemail. Text = Doc. selectsinglenode ("// user [name = '" + ddlname. selecteditem. Text + "']"). childnodes. Item (2). innertext;
}
Private void btndelete_click (Object sender, system. eventargs E)
{// Delete
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath (". // dB // dbguest. xml "));
Xmlnodelist xnl = xmldoc. selectsinglenode ("dbguest"). childnodes;
Foreach (xmlnode Xn in xnl)
{
Xmlelement Xe = (xmlelement) xn;
Xmlnodelist node = Xe. getelementsbytagname ("name ");
If (node. Count> 0)
{
If (node [0]. innertext = ddlname. selecteditem. Text)
Xe. removeall (); // delete all content of the node
Break;
}
}
Xmldoc. Save (server. mappath (". // dB // dbguest. xml "));
BIND ();
}
Private void btnadd_click (Object sender, system. eventargs E)
{// Add
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath (". // dB // dbguest. xml "));
Xmlnode root = xmldoc. selectsinglenode ("dbguest"); // search for <dbguest>
Xmlelement xe1 = xmldoc. createelement ("user"); // create a <user> node
Xmlelement xesub1 = xmldoc. createelement ("name ");
Xesub1.innertext = "guset"; // set a text node
Xe1.appendchild (xesub1); // Add it to the <user> node
Xmlelement xesub2 = xmldoc. createelement ("city ");
Xesub2.innertext = "Shanghai ";
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("email ");
Xesub3.innertext = "ss@22.net ";
Xe1.appendchild (xesub3 );
Root. appendchild (xe1); // Add it to the <dbguest> node.
Xmldoc. Save (server. mappath (". // dB // dbguest. xml "));
BIND ();
}
Private void btnchange_click (Object sender, system. eventargs E)
{// Query
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (server. mappath (". // dB // dbguest. xml "));
Xmlnodelist nodelist = xmldoc. selectsinglenode ("dbguest"). childnodes; // retrieve all child nodes of the dbguest Node
Foreach (xmlnode Xn in nodelist) // traverses all subnodes
{
Xmlelement Xe = (xmlelement) xn; // converts the subnode type to the xmlelement type
Xmlnodelist node = Xe. getelementsbytagname ("name ");
If (node. Count> 0)
{
If (node [0]. innertext = ddlname. selecteditem. Text)
{
Xmlnodelist NLS = Xe. childnodes; // continue to obtain all the child nodes of the Xe subnode
Foreach (xmlnode xn1 in NLS) // traverse
{
Xmlelement xe2 = (xmlelement) xn1; // Conversion Type
If (xe2.name = "email") // If you find
{
Xe2.innertext = tbnewmail. Text; // modify
Break; // find and exit.
}
}
Break;
}
}
}
Xmldoc. Save (server. mappath (". // dB // dbguest. xml "));
BIND ();
}
XML file Verification
Private void page_load (Object sender, system. eventargs E)
{
Filestream stream = new filestream (server. mappath ("People. xml"), filemode. Open );
// Create an object of the xmlvalidatingreader class
Xmlvalidatingreader Vr = new xmlvalidatingreader (stream, xmlnodetype. element, null );
// Load the XML architecture document
VR. schemas. Add (null, server. mappath ("People. XSD "));
// The verification method is based on the XML architecture.
VR. validationtype = validationtype. Schema;
VR. validationeventhandler + = new validationeventhandler (validationhandler );
// Verify the document
While (Vr. Read ());
// Display that the verification process is complete
Page. response. Write ("<B> Validation finished! <B> ");
// Close the opened file
Stream. Close ();
}
Private void validationhandler (Object sender, validationeventargs ARGs)
{
// Display the verification failure message
Page. response. Write ("<B> Validation Error: </B>" + args. Message + "<p> ");
}
XML transfer image
Use of the xmldocument class
Byte [] bt = new byte [size];
String strdata;
Convert a byte group to a string:
-String STR = convert. tobase64string (BT );
Converts a string to a string.
-Bt = convert. frombase64string (strdata );
Private void btnupload_click (Object sender, system. eventargs E)
{
// Get the file name to be uploaded
String strfilepathname = lofile. postedfile. filename;
String strfilename = path. getfilename (strfilepathname );
Int filelength = lofile. postedfile. contentlength;
If (filelength <= 0)
Return;
Try
{
Byte [] filebytearray = new byte [filelength]; // temporary storage of byte Arrays for image files
Stream streamobject = lofile. postedfile. inputstream; // create a data stream object
// Read image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
Streamobject. Read (filebytearray, 0, filelength );
String filename = server. mappath (". // writexml. xml"); // the file to be opened
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );
Xmlnode root = xmldoc. selectsinglenode ("dbimage"); // search for <dbguest>
Xmlnodelist xnl = xmldoc. selectsinglenode ("dbimage"). childnodes;
Int nindex = xnl. count;
// Add the following Node
Xmlelement xe1 = xmldoc. createelement ("image"); // create a <user> node
Xmlelement xesub1 = xmldoc. createelement ("imageid ");
Xesub1.innertext = nindex. tostring (); // set the text node
Xe1.appendchild (xesub1); // Add it to the <user> node
Xmlelement xesub2 = xmldoc. createelement ("imagecontenttype ");
Xesub2.innertext = lofile. postedfile. contenttype;
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("imagesize ");
Xesub3.innertext = filelength. tostring ();
Xe1.appendchild (xesub3 );
Xmlelement xesub4 = xmldoc. createelement ("imagedescription ");
Xesub4.innertext = tbdescription. text;
Xe1.appendchild (xesub4 );
Xmlelement xesub5 = xmldoc. createelement ("imagedata ");
Xesub5.innertext = convert. tobase64string (filebytearray );
Xe1.appendchild (xesub5 );
Root. appendchild (xe1); // Add it to the <dbguest> node.
Xmldoc. Save (filename );
Response. Redirect ("showallimg. aspx ");
}
Catch
{
}
}
Private void page_load (Object sender, system. eventargs E)
{
String filename = server. mappath ("./writexml. xml"); // the file to be opened
Dataset DS = new dataset ();
DS. readxml (filename );
Dgshow. datasource = Ds. Tables ["image"]. defaultview;
Dgshow. databind ();
}
Private void page_load (Object sender, system. eventargs E)
{
Int imgid = convert. toint32 (request. querystring ["ID"]); // ID is the image ID
// Create a database link
String filename = server. mappath (". // writexml. xml"); // the file to be opened
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );
Xmlnodelist node = xmldoc. selectsinglenode ("// image [imageid = '" + imgid. tostring () + "']"). childnodes;
If (node! = NULL)
{
String strtype = node. Item (1). innertext;
String strdata = node. Item (4). innertext;
Int nsize = int. parse (node. Item (2). innertext );
Response. contenttype = strtype; // set the output file type
// Binary number of output image files
Response. outputstream. Write (convert. frombase64string (strdata), 0, nsize );
Response. End ();
// It can also be saved as an image
// Filestream FS = new filestream (@ "C:/AA. BMP", filemode. openorcreate, fileaccess. Write );
// Fs. Write (convert. frombase64string (strdata), 0, nsize );
// Fs. Close ();
}
}