日期:2014-05-18 浏览次数:20836 次
XmlDocument xml = new XmlDocument(); xml.Load("1.xml"); XmlNodeList xl = xml.SelectNodes(@"A/B/C"); bool bfound = false; XmlNode xnfd = null; foreach (XmlNode xn in xl) { if(xn.InnerText.Equals("c")) { bfound = true; xnfd = xn; break; } } if(bfound) { xnfd.InnerText = "newc"; xml.Save("1.xml"); } else { //没有,加新的 }
------解决方案--------------------
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("XMLFile1.xml"); var elmtsB = xmlDoc.SelectNodes("//B"); foreach (XmlNode elB in elmtsB) { foreach (XmlNode el in elB.ChildNodes) { if (el.Name == "C") { Console.WriteLine("It has C"); break; } } }