Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Delete InnerText with its node. [modified]

Delete InnerText with its node. [modified]

Scheduled Pinned Locked Moved XML / XSL
csharpxmltutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eawedat
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • E eawedat

      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

      N Offline
      N Offline
      Nisha Agrawal
      wrote on last edited by
      #2

      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.

      E 1 Reply Last reply
      0
      • N Nisha Agrawal

        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.

        E Offline
        E Offline
        eawedat
        wrote on last edited by
        #3

        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.

        E N 2 Replies Last reply
        0
        • E eawedat

          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.

          E Offline
          E Offline
          eawedat
          wrote on last edited by
          #4

          http://img207.imageshack.us/img207/2585/snippet.gif[^] http://img39.imageshack.us/img39/1164/err3.gif[^]

          1 Reply Last reply
          0
          • E eawedat

            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.

            N Offline
            N Offline
            Nisha Agrawal
            wrote on last edited by
            #5

            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.

            E 1 Reply Last reply
            0
            • N Nisha Agrawal

              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.

              E Offline
              E Offline
              eawedat
              wrote on last edited by
              #6

              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:)))

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups