Results 1 to 4 of 4

Thread: [2.0] Deleting a node from XML

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [2.0] Deleting a node from XML

    Hi all:

    I'm trying to do some manipulation with an xml file. What I'm doing is trying to store and manipulate a small amount of data, but not with a database but an XML file. So the following xml file simply saves settings each with a name, head and tail value. For each new setting, there is another setting node inserted.

    Code:
    <?xml version="1.0"?>
    <PDFBUILDER>
      <RENAMESETTINGS>
        <SETTING>
          <NAME Type="Name">TEST</NAME>
          <HEAD Type="Head">df</HEAD>
          <TAIL Type="Tail">def</TAIL>
        </SETTING>
        <SETTING>
          <NAME Type="Name">2TEST</NAME>
          <HEAD Type="Head">esser</HEAD>
          <TAIL Type="Tail">deesr</TAIL>
        </SETTING>
        <SETTING>
          <NAME Type="Name">bbdos</NAME>
          <HEAD Type="Head">ewr</HEAD>
          <TAIL Type="Tail" />
    	</SETTING>
      </RENAMESETTINGS>
    </PDFBUILDER>

    The problem comes in when I want to user to delete a node / setting. Say for e.g., if the user wanted to delete the setting named: 2TEST, is there anyway to read in the xml from the file, and delete the node:

    <SETTING>
    <NAME Type="Name">2TEST</NAME>
    <HEAD Type="Head">esser</HEAD>
    <TAIL Type="Tail">deesr</TAIL>
    </SETTING>

    And then write back the result to the file? Thus what I want to write back to the file will be:

    <?xml version="1.0"?>
    <PDFBUILDER>
    <RENAMESETTINGS>
    <SETTING>
    <NAME Type="Name">TEST</NAME>
    <HEAD Type="Head">df</HEAD>
    <TAIL Type="Tail">def</TAIL>
    </SETTING>
    <SETTING>
    <NAME Type="Name">bbdos</NAME>
    <HEAD Type="Head">ewr</HEAD>
    <TAIL Type="Tail" />
    </SETTING>
    </RENAMESETTINGS>
    </PDFBUILDER>


    Now I know how to write to xml files etc. what I don't know is how to extract and delete a node for e.g. 2TEST.

    Could anyone help me with this?

    Jennifer.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] Deleting a node from XML

    First my disclaimer that I'm just learning about XML myself, so take this with a grain of salt.
    There may be a better way but this seems to work:

    c# Code:
    1. // XML
    2.         private void button1_Click(object sender, EventArgs e)
    3.         {  
    4.             XmlDocument d = new XmlDocument();
    5.             d.Load(@"c:\XMLFile1.xml");
    6.             XmlNodeList nodList = d.SelectNodes("PDFBUILDER/RENAMESETTINGS/SETTING/NAME[@Type='Name']");
    7.             foreach (XmlNode nod in nodList)
    8.             {
    9.                 if (nod.InnerXml.Equals("2TEST"))
    10.                     nod.ParentNode.ParentNode.RemoveChild(nod.ParentNode);
    11.             }
    12.             d.Save(@"c:\XMLFile1.xml");
    13.         }

    Good luck.

  3. #3

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [2.0] Deleting a node from XML

    Hey thanks! I tried it and it worked fine. Just one question, I solved my problem but is there a way to insert a node similar to the way you gave to me?

    Jennifer.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] Deleting a node from XML

    Hi Jennifer,
    I can't say that I've tried that out yet, but perhaps this link will help: http://www.developerfusion.co.uk/show/3227/

    Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width