Delete InnerText with its node. [modified]
-
hey all. I have this code <albume1> <file>Mortal.mp3</file> <file>Seek.mp3</file> </albume1> and need function which is written in C#.NET to do these things gets name of file as parameter/ void RemoveLine(string name) { searchs in albume1 the name lets take for example "Seek.mp3" found one delete the whole line <file>Seek.mp3</file> } so the final result/save of XML file would be : <albume1> <file>Mortal.mp3</file> </albume1> I tried with InnerText attribute but InnerText takes whole Textes which appear among the two tags thanks
modified on Tuesday, October 6, 2009 3:54 PM
-
hey all. I have this code <albume1> <file>Mortal.mp3</file> <file>Seek.mp3</file> </albume1> and need function which is written in C#.NET to do these things gets name of file as parameter/ void RemoveLine(string name) { searchs in albume1 the name lets take for example "Seek.mp3" found one delete the whole line <file>Seek.mp3</file> } so the final result/save of XML file would be : <albume1> <file>Mortal.mp3</file> </albume1> I tried with InnerText attribute but InnerText takes whole Textes which appear among the two tags thanks
modified on Tuesday, October 6, 2009 3:54 PM
First of all, i would say that you have to remove a node not it's innertext. So you need to find the node. Second thing, IS all the album node is named as albume1 or is it the name of the Alubum? anyways you can remove the child node in this way.
XmlDocument _xmlDoc = new XmlDocument();
if (System.IO.File.Exists(xmlDbFilepath))
{
_xmlDoc.Load(xmlDbFilepath);
}XmlNode albumNode = _xmlDoc.SelectSingleNode("/root/album");
if (albumNode != null)
{
foreach (XmlNode node in albumNode.ChildNodes)
{
if (node.InnerText.trim() == fileName)
{
albumNode.RemoveChild(node);
}
}
}I hope it will help you.
-
First of all, i would say that you have to remove a node not it's innertext. So you need to find the node. Second thing, IS all the album node is named as albume1 or is it the name of the Alubum? anyways you can remove the child node in this way.
XmlDocument _xmlDoc = new XmlDocument();
if (System.IO.File.Exists(xmlDbFilepath))
{
_xmlDoc.Load(xmlDbFilepath);
}XmlNode albumNode = _xmlDoc.SelectSingleNode("/root/album");
if (albumNode != null)
{
foreach (XmlNode node in albumNode.ChildNodes)
{
if (node.InnerText.trim() == fileName)
{
albumNode.RemoveChild(node);
}
}
}I hope it will help you.
thanks for helping i will try the code. with regards to ur Q. albume1 is name of the album so there will be also albume2 and albume3 and all those tags are subs.. of main tag which is <albumes> this how my xml file looks like: <?xml version="1.0" encoding="utf-8"?> <albumes> <albume2> <file>soso</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </albume2> <koko> <file>soso</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </koko> <Roro> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </Roro> </albumes> with regards to this line : XmlNode albumNode = _xmlDoc.SelectSingleNode("/root/album"); why have u used root's word? i have no roots word in there u meant it as Theory? or what i cannot get it can you replace the line please after that i gave u clear idea about the xml.
-
thanks for helping i will try the code. with regards to ur Q. albume1 is name of the album so there will be also albume2 and albume3 and all those tags are subs.. of main tag which is <albumes> this how my xml file looks like: <?xml version="1.0" encoding="utf-8"?> <albumes> <albume2> <file>soso</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </albume2> <koko> <file>soso</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </koko> <Roro> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </Roro> </albumes> with regards to this line : XmlNode albumNode = _xmlDoc.SelectSingleNode("/root/album"); why have u used root's word? i have no roots word in there u meant it as Theory? or what i cannot get it can you replace the line please after that i gave u clear idea about the xml.
-
thanks for helping i will try the code. with regards to ur Q. albume1 is name of the album so there will be also albume2 and albume3 and all those tags are subs.. of main tag which is <albumes> this how my xml file looks like: <?xml version="1.0" encoding="utf-8"?> <albumes> <albume2> <file>soso</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </albume2> <koko> <file>soso</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </koko> <Roro> <file>D:\test.mp3</file> <file>D:\test.mp3</file> </Roro> </albumes> with regards to this line : XmlNode albumNode = _xmlDoc.SelectSingleNode("/root/album"); why have u used root's word? i have no roots word in there u meant it as Theory? or what i cannot get it can you replace the line please after that i gave u clear idea about the xml.
Actually you havn't provided the whole xml so i have taken "root" as placeholder. For your case it would be "albumes". Also in "/root/album" is the placeholder for the album name.
-
Actually you havn't provided the whole xml so i have taken "root" as placeholder. For your case it would be "albumes". Also in "/root/album" is the placeholder for the album name.
Thanks a lot. I believe in ur codes but in my project actually did not work. even so its a good code. am not aware of the reason by the way i changed the whole idea. besides removing nodes and staff i used re-write method. to be more clear. I used an array(List) to help lets say i have album named "Koko" and down to it has 2 songs blah1 blah2 and wanna delete song named "blah2" i insert two songs first to an array when click remove song,, it goes to this array and delete existing song. and call a function that writes info from this array to xml file. so xml file will be updated with a new info from that array. thanks anyway:)))