Xml
. NET XML
1.XML is a markup language that provides a description of the data format. Its self description makes it very adaptable to data exchange between different applications, and this exchange is not based on a predefined group data structure!
eg
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<course_information>
<courseName> Operating System </courseName>
<courseTeacher> Zou </courseTeacher>
<coursePath>Database.aspx</coursePath>
<courseAddTime>2006-7-1</courseAddTime>
</course_information>
<course_information>
<courseName> Database System Principles </courseName>
<courseTeacher> Hu </courseTeacher>
<coursePath>Database.aspx</coursePath>
<courseAddTime>2006-7-2</courseAddTime>
</course_information>
<course_information>
<courseName> Data Structure </courseName>
<courseTeacher> Lee </courseTeacher>
<coursePath>Database.aspx</coursePath>
<courseAddTime>2006-7-30</courseAddTime>
</course_information>
</root>
2. Edit XML Document
First read the course_information.xml into memory, form a DOM tree, and then locate the parent node that you want to insert, and add a new
Node to add a new node, which is a new element of the XML document. Finally, the DOM tree in memory is saved as an XML document.
eg
To add a new node:
String st= "database.aspx";
DateTime Date1=datetime.now;
String date=date1. ToString ("D");
XmlDocument xmldoc=new XmlDocument ();
Xmldoc.load (file);
XmlNode root=xmldoc.selectsinglenode ("root");//select the root node;
XmlElement course=xmldoc.createelement ("course_information");
XmlElement name=xmldoc.createelement ("Coursename");
Name. Innertext=this. TextBox1.Text;
XmlElement teacher=xmldoc.createelement ("Courseteacher");
Teacher. Innertext=this. TextBox2.Text;
XmlElement path=xmldoc.createelement ("Coursepath");
Path. innertext=st;
XmlElement addtime=xmldoc.createelement ("Courseaddtime");
Addtime.innertext=date;
Course. AppendChild (name);
Course. AppendChild (teacher);
Course. AppendChild (path);
Course. AppendChild (Addtime);
Root. AppendChild (course);
Xmldoc.save (file);
Xmldoc=null;
To delete a node:
String Filename=server.mappath (@ ". \xml\course_information.xml");
XmlDocument xmldoc=new XmlDocument ();
Xmldoc.load (filename);
String Name=this. TextBox1.Text.ToString ();
XmlNode oldnode=xmldoc.selectsinglenode ("Root/course_information[coursename= '" +name+ ")";
XmlNode root=xmldoc.selectsinglenode ("root");
Root. RemoveChild (OldNode);
Xmldoc.save (filename);
Xmldoc=null;
Dt=op. READXML (filename);
Dv=dt. DefaultView;
Bindgrid ()//bind to DataGrid