开发者

Deleting node from XML

I'm very close. I'm ask to delete an entry from an XML FILE if the last name of an ASP TEXT BOX matches to an XML "entry".

Here is the button script. Please take note of XmlNode PhoneBook line:

protected void deletion_Click(object sender, EventArgs e)
{
    XmlDocument document = new XmlDocument();
    document.Load(Server.MapPath("~/App_Data/PhoneBook.xml"));

    XmlElement root = document.DocumentElement;

XmlNode PhoneBook = document.SelectSingleNode("//event[@lastName='" + txtLastName.Text + "']");
    PhoneBook.ParentNode.RemoveChild(PhoneBook);

    document.Save(Server.MapPath("~/App_Data/PhoneBook开发者_Go百科.xml"));
    GridView1.DataBind();
}

I keep getting errors, I'm guessing I'm not selecting the correct node in the xml file using SelectSingleNode?


Try this:

XmlNode PhoneBook = document.SelectSingleNode("/phoneBook/entry[lastName/text()='" + txtLastName.Text + "']");

You're trying to filter on an element, not an attribute so you can't use the @ sign.

This XPath returns the 2nd entry element (I tested it, it works):

/phoneBook/entry[lastName/text()='twoL']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜