C#—XML读取、增加、修改和删除操作
A版本:
1.xml文件格式如下:
<?xml version="1.0" encoding="utf-8"?>
<projects>
<project name="PlatformFramewo" vss-path="Platform$/Source CodHdt$Pla~1.sln" />
</projects>
1.读取
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(@"Projects.xml"));
DataTable dt = ds.Tables[0];
return dt;
//得到的datable在前台进行循环输出,省略...
<tr style="font-weight: bold;"> //文字加粗
<td style="border-bottom: solid 2px gray;"> //文字底部加横线
2.新增
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.修改
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.删除
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);
}
}
B版本:
//添加xml节点
private void AddXml(string image, string title)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../flash/dati.xml"));
XmlNode root = xmlDoc.SelectSingleNode("images");//查找<images>
XmlElement xe1 = xmlDoc.CreateElement("thumb");//创建一个<thumb>节点
xe1.SetAttribute("displayNum", "6");//设置该节点displayNum属性
xe1.SetAttribute("separation", "5");//设置该节点separation属性
XmlElement xesub1 = xmlDoc.CreateElement("image");
xesub1.InnerText = image;//设置文本节点
xe1.AppendChild(xesub1);//添加到thumb节点中
XmlElement xesub2 = xmlDoc.CreateElement("description");
xesub2.InnerText = title;
xe1.AppendChild(xesub2);
root.AppendChild(xe1);//添加到<images>节点中
xmlDoc.Save(Server.MapPath("../flash/dati.xml"));
}
//删除节点内容
private void DelXml(string image)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("../flash/dati.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("images").ChildNodes; //查找节点
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.InnerText.IndexOf(image,0) >= 0)
{
xn.ParentNode.RemoveChild(xn);
// xn.RemoveAll();
}
}
xmlDoc.Save(Server.MapPath("../flash/dati.xml"));
}
//修改
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="")// 判读条件
{
xe.SetAttribute("genre",newStr);//则修改该属性为newstr
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到 //判读条件
{
xe2.InnerText=newText;//则修改
break;//找到退出来就可以了
}
}
break;
}