- <
- < - <RadioBar>
<Genre_Genre_Country>Genre_Country</Genre_Genre_Country>
- <Genre_Genre_Country>
<Title>Boot Liquor Radio - Dysfunctional Country Station, Genre</Title>
<url>http://64.72.118.137:7050</url>
</Genre_Genre_Country>
- <Genre_Genre_Country>
<Title>Star104 - Today's Best Country!</Title>
<url>http://67.159.26.2:9794</url>
</Genre_Genre_Country>
- <Genre_Genre_Country>
<Title>Classic Heartland - Classic Country, Western, Bluegrass, Alternative Country [Broadband]</Title>
<url>http://130.166.72.1:8006</url>
</Genre_Genre_Country>
- <Genre_Genre_Country>
<Title>S K Y . F M - Country - today's Hit Country with a mix of your favorites</Title>
<url>http://64.236.34.4:80/stream/1019</url>
</Genre_Genre_Country>
</RadioBar>
Whats the easiest way to delete a node, say: Find the node by the Title...and delete the Title and url (the whole node)
F
F
F
FinF

Delete XML node
Vijay R
PolatPinar
doc.SelectSingleNode ("/RadioBar").RemoveNode (node);
no definition for RemoveNode....
XmlDocument doc = new XmlDocument();doc.Load(
Application.StartupPath.ToString() + "\\RadioBarPresets.xml"); string NodeWeWantToDelete = "S K Y . F M - Country - today's Hit Country with a mix of your favorites"; XmlNode node = doc.SelectSingleNode(string.Format("//*[Title=\"{0}\"]", NodeWeWantToDelete)); if (node != null)doc.SelectSingleNode(
"/RadioBar").....Console.WriteLine(
"done");doc.Save(
Application.StartupPath.ToString() + "\\RadioBarPresets.xml");nglow
Sorry... it's RemoveChild(). I'll copy and paste this time, as it seems I cannot trust my brains this late.
string NodeWeWantToDelete = "Delete Me!";XmlNode node = doc.SelectSingleNode (string.Format ("//*[Title=\"{0}\"]", NodeWeWantToDelete));
if (node != null)
doc.SelectSingleNode ("/RadioBar").RemoveChild (node);
HTH
--mc
scripteaze
Hmm, works here. I used the following simple program:
class Program {static string originalDocument = @"
<RadioBar>
<Genre_Genre_Country>Genre_Country</Genre_Genre_Country>
<Genre_Genre_Country>
<Title>Boot Liquor Radio - Dysfunctional Country Station, Genre</Title>
<url>http://64.72.118.137:7050</url>
</Genre_Genre_Country>
<Genre_Genre_Country>
<Title>Star104 - Today's Best Country!</Title>
<url>http://67.159.26.2:9794</url>
</Genre_Genre_Country>
<Genre_Genre_Country>
<Title>Delete Me!</Title>
<url>http://130.166.72.1:8006</url>
</Genre_Genre_Country>
<Genre_Genre_Country>
<Title>S K Y . F M - Country - today's Hit Country with a mix of your favorites</Title>
<url>http://64.236.34.4:80/stream/1019</url>
</Genre_Genre_Country>
</RadioBar>"; static void Main (string [] args) {
XmlDocument doc = new XmlDocument ();
doc.LoadXml (originalDocument);
string NodeWeWantToDelete = "Delete Me!";
XmlNode node = doc.SelectSingleNode (string.Format ("//*[Title=\"{0}\"]", NodeWeWantToDelete));
if (node != null)
doc.FirstChild.RemoveChild (node);
Console.WriteLine ("done");
}
}
It would seem that the selection is getting the wrong node. Care to post your code
--mc
CharlesF
I was afraid of something like that...unfortunately I can't change the xml formatting now...it would break too much.
No way to just search for the title string and delete the node its in dang.
Will Merydith
hi,
yes you can , replace this line
XmlNode nod = root.SelectSingleNode("Genre_Genre_Country[Title = \"S K Y . F M - Country - today's Hit Country with a mix of your favorites\"]");but in general to put id node or attribute is always better at least for me
hope this helps
manqueInspiration
Thanks Mario. I'm getting:
System.InvalidOperationException: The current node cannot contain other nodes, so the node to be removed is not its child.
ravindranathw
cablehead,
XmlDocument.LoadXml expects a string containing the Xml document, not a path to a file. This explains why it complains about your call. You would have to use the Load() method to use it that way.
Anyway, I think I got what the problem is. In your original post, the xml didn't have an xml header, so doc.FirstChild would have worked. If your file begins with: < xml version... then that is going to be the first node of the document and this would explain your exception.
The problem is easily cured:
from the code I posted before, change:
doc.FirstChild.RemoveNode (node);
to
doc.SelectSingleNode ("/RadioBar").RemoveNode (node);
this way we will be sure to use RadioBar as the root, regardless of how the file is formatted.
HTH
--mc
mickdelaney
cablehead,
the quickest I can think of is:
XmlDocument doc ... // this contains the document
XmlNode node = doc.SelectSingleNode (string.Format ("//*[Title=\"{0}\"]", titleWeWantToDelete);
if (node != null)
doc.FirstChild.RemoveChild (node);
HTH
--mc
iamman
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
XmlDocument doc = new XmlDocument();doc.LoadXml(
Application.StartupPath.ToString() + "\\RadioBarPresets.xml"); string NodeWeWantToDelete = "S K Y . F M - Country - today's Hit Country with a mix of your favorites"; XmlNode node = doc.SelectSingleNode(string.Format("//*[Title=\"{0}\"]", NodeWeWantToDelete)); if (node != null)doc.FirstChild.RemoveChild(node);
Console.WriteLine("done");NewbieDude
Thanks again. That did the trick.
Now I have to figure how to add a node...anywhere within a Genre_Genre_Whatever
MaryAnn80
hi,
you can do that by using remove child method, the problem is in how to select a node to remove it according to your xml all your fields are strings, xml strings has escape chars you have to remplace it with some numbers and its very silly thing to do , so i suggest to add another attribute ,to filter according to it you will add it to every <Genre_Genre_Country ID="2"> node
so the code will be something like this
XmlDocument doc = new XmlDocument();
doc.Load("xmlfile1.xml");
XmlNode root = doc.SelectSingleNode("RadioBar");
XmlNode nod = root.SelectSingleNode("Genre_Genre_Country[@ID = 4]");
root.RemoveChild(nod);
doc.Save(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"/file.xml");
i saved in different location for not currupting the origional file but you can save to the same location
hope this helps