在ASP.NET中用XmlDocument如何删除XML文件中的某一结点?
是这样的,我用下面的程序可以删除节点 <User> </User> 中的所有内容,但是我却无法删除 <User> </User> 节点
protected void DeleteButton_Click(object sender, EventArgs e)
{
XmlDocument Doc = new XmlDocument();
Doc.Load(Server.MapPath( ".\\db\\dbGuest.xml "));
XmlNodeList NodeList = Doc.SelectSingleNode( "dbGuest ").ChildNodes;
foreach (XmlNode xn in NodeList)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName( "Name ");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
{
xe.RemoveAll();
break;
}
}
//xn.RemoveAll();
}
Doc.Save(Server.MapPath( ".\\db\\dbGuest.xml "));
DataBind();
}
XML文件:
<?xml version= "1.0 " standalone= "yes "?>
<dbGuest>
<User>
<Name> asfew3ere1sfasf </Name>
</User>
<User>
<Name> asfew3243232cvd1sfasf </Name>
</User>
</dbGuest>
例如我按下了DeleteButton按钮要删除第二个 <User> </User> 我想得的的XML文件是这样的:
<?xml version= "1.0 " standalone= "yes "?>
<dbGuest>
<User>
<Name> asfew3ere1sfasf </Name>
</User>
</dbGuest>
但是我实际上得到的结果是这样的:
<dbGuest>
<User>
<Name> asfew3ere1sfasf </Name>
</User>
<User>