C#—XML讀取、增加、修改、刪除操作
1.xml檔案格式如下:
1.讀取
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(@ "Projects.xml"));
DataTable dt = ds.Tables[0];
return dt;
//得到的datable在前台進行循環輸出,省略...
2.新增
XmlDocument xmlDoc = new XmlDocument();
string Path = Server.MapPath(@"Projects.xml");
xmlDoc.Load(Path);
XmlNode root=xmlDoc.SelectSingleNode("projects");
XmlElement xe1 = xmlDoc.CreateElement("project");
XmlElement xe1 = xmlDoc.CreateElement("project")
XmlElement xe1 = xmlDoc.CreateElement("project"); ("name", txtProjectName.Text);
strVssPath = txtProjectVss.Text + "$" + txtProjectPath.Text + "$" + txtProjectSln.Text;
xe1.SetAttribute("vss-path",strVssrootPath",strVssroot); AppendChild(xe1);
xmlDoc.Save(Path);
3.修改
XmlDocument xmlDoc = new XmlDocument();
string Path = Server.MapPath(@"Projects.xml");
string Path = Server.MapPath(@"Projects.xml"); ;
XmlNodeList nodelist = xmlDoc.SelectSingleNode("projects").ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement)xn; "name"].ToString())
{
xe.SetAttribute("name", txtProjectName1.Text);
txtProjectSln1.Text;
xe .SetAttribute("vss-path", strVssPath);
xmlDoc.Save(Path);
}
}
mlmlml
xmlDoc.Load(Path);
XmlNodeList nodelist = xmlDoc.SelectSingleNode("projects").ChildNodes;
foreach (XmlNode xn in nodelist)
{
Xmlxn ( .GetAttribute("name") == Request["name"].ToString())
{
xn.ParentNode.RemoveChild(xn);