1. Understand the structure of dataset loading XML files
2. Read, modify, and delete XML node instances
3. A little description andCodeDownload
<1> understand the structure of the XML file loaded by dataset.
If the following XML file is loaded into Ds, use the debugging window of Vs to obtain the structure of the DS file:
<? XML version = "1.0" encoding = "UTF-8" ?>
< Bookstore >
< Book Genre = "Fantasy" ISBN = "1-3631-4" >
< Title > Oberon's legacy </ Title >
< Author > Corets, Eva </ Author >
< Price > 5.95 </ Price >
</ Book >
</ Bookstore>
Through the above observation, we can see that when DS loads an XML file, the node attributes (suchGenre) Load all data to datarow.
<2>. Read, modify, and delete an XML node instance.
To read the above XML file, the modification operation is relatively simple, just some operations on DS: the key code is as follows:
Dataset DS = new dataset ();
// Read data
DS. readxml ("./bookstore. xml ");
// Display data
Console. writeline (Ds. Tables [0]. Rows [0] ["title"]);
// you need to read the data cyclically, find the data, and modify the data.
DS. tables [0]. rows [0] ["title"] = "Change title";
// insert data
datarow ROW = Ds. tables [0]. newrow ();
// Add data to the row
row ["genre"] = "genre ";
row ["ISBN"] = "ISBN";
row ["title"] = "title ";
row ["author"] = "author";
row ["price"] = "price";
DS. tables [0]. rows. add (ROW);
// Delete data
Datarow r = Ds. Tables [0]. Rows [0];
DS. Tables [0]. Rows. Remove (R );
// Save the modified data
DS. writexml ("./bookstore. xml ");
Console. readkey ();
The above code can solve the problem of existing nodes in XML. If there are no nodes in the original XML file, you can only add them manually. You need to write the code manually. Below is a simple example:
DS. readxml (xmlpath );
// If the row is empty and a new row is added, if (Ds. tables. count = 0) {datatable dt = new datatable ("item"); datacolumn Dc = new datacolumn ("item_url", typeof (string); DC. columnmapping = mappingtype. attribute; DT. columns. add (DC); DC = new datacolumn ("Link", typeof (string); DC. columnmapping = mappingtype. attribute; DT. columns. add (DC); DC = new datacolumn ("itemtitle", typeof (string); DC. columnmapping = mappingtype. attribu Te; DT. columns. add (DC); datarow ROW = DT. newrow (); row ["item_url"] = "homepagebanners/" + ID + extension; row ["Link"] = ". /viewpicturenews. aspx? Id = "+ ID; row [" itemtitle "] = string. Empty; DT. Rows. Add (ROW); DS. Tables. Add (DT );}
Generate an XML file in the following format:
<? XML version = "1.0" standalone = "yes" ?>
< Bcaster >
< Item Item_url = "Homepagebanners/4e504e3e-a30b-47108acfd-2a07237dc9e2.jpg" Link = "./Viewpicturenews. aspx? Id = 4e504e3e-a30b-47417acfd-2a07237dc9e2" Itemtitle = "" />
</Bcaster>
<3>. Some instructions and code downloads
Because the above method needs to re-generate an XML file (Ds. writexml (". /bookstore. XML ");), that is, to overwrite the original file, so the above method has better performance for smaller files, while reducing the use of system. in the XML namespaceProgramError.
/Files/xuqiang/CSHARP/xmlreader_writer.zip