C#-xml read, add, modify, and delete operations
The 1.xml file format is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<projects>
<project name= "Platformframewo" vss-path= "Platform$/source Codhdt$pla~1.sln"/>
</projects>
1. Read
DataSet ds = new DataSet ();
Ds. READXML (Server.MapPath (@ "Projects.xml"));
DataTable dt = ds. Tables[0];
return DT;
The resulting datable in front of the loop output, omitted ...
<tr style= "Font-weight:bold;" >//Text Bold
<TD style= "Border-bottom:solid 2px Gray;" >//Text bottom plus Horizontal
2. New
XmlDocument xmldoc = new XmlDocument ();
String Path = Server.MapPath (@ "Projects.xml");
Xmldoc.load (Path);
XmlNode Root=xmldoc.selectsinglenode ("projects");
XmlElement xe1 = xmldoc.createelement ("project");
Xe1. SetAttribute ("name", Txtprojectname.text);
Strvsspath = Txtprojectvss.text + "$" + Txtprojectpath.text + "$" + txtprojectsln.text;
Xe1. SetAttribute ("Vss-path", Strvsspath);
Root. AppendChild (XE1);
Xmldoc.save (Path);
3. Modify
XmlDocument xmldoc = new XmlDocument ();
String Path = Server.MapPath (@ "Projects.xml");
Xmldoc.load (Path);
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Projects"). ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement) xn;
if (XE). GetAttribute ("name") = = = request["Name"]. ToString ())
{
Xe. SetAttribute ("name", Txtprojectname1.text);
Strvsspath = Txtprojectvss1.text + "$" + Txtprojectpath1.text + "$" + txtprojectsln1.text;
XE. SetAttribute ("Vss-path", Strvsspath);
Xmldoc.save (Path);
}
}
4. Delete
XmlDocument xmldoc = new XmlDocument ();
String Path = Server.MapPath (@ "Projects.xml");
Xmldoc.load (Path);
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Projects"). ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement XE = (XmlElement) xn;
if (XE. GetAttribute ("name") = = = request["Name"]. ToString ())
{
Xn. Parentnode.removechild (xn);
Xmldoc.save (Path);
}
}