I am trying to delete an element on a specific match between the value from a textbox and from my Xml file. This is my code and I am getting an error in:
string x = xnode.SelectSingleNode("Url").InnerText.ToString();
XmlDocument favourites = new XmlDocument();
favourites.Load("Favourites.xml");
foreach (XmlNode xnode in favourites.SelectNodes("Favourite/MyFavourite/Url"))
{
string x = xnode.SelectSingleNode("Url").InnerText.ToString();
if (x == Url)
{
// xnode.ParentNode.ReplaceChild(newchild,oldChild);
xnode.ParentNode.RemoveChild(xnode);
}
}
this is my xml:
"<?xml version="1.0" encoding="utf-8"?>
<Favourite>
<MyFavourite>
<name>Haider</name>
<Url>http://www.yahoo.com</Url>
</MyFavourite>
<MyFavourite>
<name>Haider</name>
<Url>http://www.gmail.com</Url>
</MyFavourite>
<MyFavourite>
<name>Haider</name>
<Url>http://www.naji.com</Url>
</MyFavourite>
<MyFavourite>
<name>Haider</name>
<Url>http://www.yahoo.com</Url>
</MyFavourite>
<MyFavourite>
<name>Haider</name>
<Url>http://www.yahoo.com</Url>
</MyFavourite>
<MyFavourite>
<name>Haider</name>
<Url>http://www.yahoo.com</Url>
</MyFavourite>
</Favourite>"
The varialbe
xnodepoints already to the URL element. Just skip the second xpath query:But this means that you have to update the remove part of your code since the xnode does not point to the MyFavourite element: