string filepath = "" ; // Your file path
XmlDocument doc = new XmlDocument();
doc.Load(filepath);
XmlNode xn = doc.ChildNodes[1].ChildNodes[1].ChildNodes[0].ChildNodes[0]; List<XmlNode> lx=new List<XmlNode>();
for (int i = 0; i < xn.ChildNodes.Count; i++)
{
lx.Add(xn.ChildNodes[i]);
} foreach (var item in lx)
{
if (item.Name=="moi"&&item.Attributes["xsi:type"].Value=="APPCERT")
{
xn.RemoveChild(item);
}
} doc.Save(filepath);
我这个比楼上那位稍微简洁了一点儿string xmlpath = @"F:\Test\Test\Test\XMLFile1.xml";
XmlDocument xml = new XmlDocument();
xml.Load(xmlpath);//加载xml文件,xmlpath为xml文件的路径
XmlNode key = xml.ChildNodes[1].ChildNodes[1].ChildNodes[0].ChildNodes[0];//<module xsi:type="LTESiteModule">
XmlNodeList nodes = key.ChildNodes;
for (int i = 0; i < nodes.Count; i++)
{
XmlElement node = (XmlElement)nodes[i];
if (node.Name == "moi" && node.GetAttribute("xsi:type") == "APPCERT")
{
key.RemoveChild(node);
i -= 1;
}
}
xml.Save(xmlpath);
XmlDocument xml = new XmlDocument();
xml.Load(xmlpath);//加载xml文件,xmlpath为xml文件的路径
for (int j = 1; j < xml.ChildNodes[1].ChildNodes.Count; j++)//遍历session
{
XmlNode key = xml.ChildNodes[1].ChildNodes[j].ChildNodes[0].ChildNodes[0];
XmlNodeList nodes = key.ChildNodes;
for (int i = 0; i < nodes.Count; i++)
{
XmlElement node = (XmlElement)nodes[i];
if (node.Name == "moi" && node.GetAttribute("xsi:type") == "APPCERT")
{
key.RemoveChild(node);
i -= 1;
}
}
}
xml.Save(xmlpath);
string xmlpath = @"F:\Test\Test\Test\XMLFile1.xml";
XmlDocument xml = new XmlDocument();
xml.Load(xmlpath);//加载xml文件,xmlpath为xml文件的路径
for (int j = 1; j < xml.ChildNodes[1].ChildNodes.Count; j++)//遍历session
{
XmlNode key = xml.ChildNodes[1].ChildNodes[j].ChildNodes[0].ChildNodes[0];
XmlNodeList nodes = key.ChildNodes;
for (int i = 0; i < nodes.Count; i++)
{
XmlElement node = (XmlElement)nodes[i];
if (node.Name == "moi" && node.GetAttribute("xsi:type") == "APPCERT")
{
key.RemoveChild(node);
i -= 1;
}
}
}
xml.Save(xmlpath);
http://blog.csdn.net/jiankunking/article/details/17992745
http://blog.csdn.net/jiankunking/article/details/17992793
可以看看这个