111,126
社区成员
发帖
与我相关
我的任务
分享
try
{
string name = "sfafasdf";//这是你要删除的电影的名称(Name的值)
XmlDocument myDoc = new XmlDocument();
myDoc.Load("TodayMovie.xml");
XmlNode oldNode = null;
XmlNode node = myDoc.DocumentElement;
foreach(XmlNode xmlNode in node.ChildNodes)
{
if(xmlNode.Name == "MovieName")
{
foreach(XmlNode movieNode in xmlNode.ChildNodes)
{
if(movieNode.Name == "name")
{
if(movieNode.InnerText = name)
{
movieNode.ParentNode.RemoveAllChild();//好像是这个吧,移除所有子项。
{
}
}
}
}
myDoc.Save("TodayMovie.xml");//保存
MessageBox.Show("电影删除成功!", "删除", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("抱歉,删除电影出现错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
/// <summary>
/// 根据节点删除记录
/// </summary>
/// <param name="strSelectNodes">节点的XPath</param>
/// <param name="strSelectSingleNode">节点的名称</param>
/// <param name="strId">节点的值</param>
/// <returns>删除成功返回True,删除失败返回False</returns>
public Boolean DeleteXmlData(String strSelectNodes, String strSelectSingleNode, String strId)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.m_xmlFilePath);
System.Xml.XmlNodeList xmlNL = xmlDoc.SelectNodes(strSelectNodes);
foreach (System.Xml.XmlNode xmlN in xmlNL)
{
XmlElement xe = (XmlElement)xmlN.SelectSingleNode(strSelectSingleNode);
if (xe != null && xe.InnerText.Equals(strId))
xmlN.ParentNode.RemoveChild(xmlN);
}
xmlDoc.Save(this.m_xmlFilePath);//保存删除后的XML文件
return true;
}
catch
{
return false;
}
}