user734699
user734699

Reputation: 23

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.xml"));
    GridView1.DataBind();
}

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

Upvotes: 0

Views: 1152

Answers (1)

Josh M.
Josh M.

Reputation: 27831

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']

Upvotes: 3

Related Questions