|
-
Jul 9th, 2007, 11:53 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Jul 9th, 2007, 12:56 PM
#2
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:
// XML
private void button1_Click(object sender, EventArgs e)
{
XmlDocument d = new XmlDocument();
d.Load(@"c:\XMLFile1.xml");
XmlNodeList nodList = d.SelectNodes("PDFBUILDER/RENAMESETTINGS/SETTING/NAME[@Type='Name']");
foreach (XmlNode nod in nodList)
{
if (nod.InnerXml.Equals("2TEST"))
nod.ParentNode.ParentNode.RemoveChild(nod.ParentNode);
}
d.Save(@"c:\XMLFile1.xml");
}
Good luck.
-
Jul 10th, 2007, 02:27 PM
#3
Thread Starter
Hyperactive Member
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.
-
Jul 10th, 2007, 02:46 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|