XML Node ?

G

Guest

Good morning,

I have the folowing XML data in a file;
<code>

<?xml version="1.0" encoding="utf-8"?>
<Photos>
<GotTogetoffThisComment>@ 2/21/2005 10:01:14 AM Visitor{IP}: 127.0.0.1
left;
She Dances good
</GotTogetoffThisComment>
<JudgeDreddComment>@ 2/21/2005 10:25:12 AM Visitor{IP}: 127.0.0.1 left;
The future Cop
</JudgeDreddComment>
<SheDancesComment>@ 2/21/2005 10:50:28 AM Visitor{IP}: 127.0.0.1 left;
Shes dances for the hutt
</SheDancesComment>
</Photos>

</code>

In VB.net how would I find the node, JudgeDreddComment, and delete it from
the file ?

Please help as this is driving me nuts.

Thanks
 
C

Christopher Kimbell

I'm not going to attempt writng VB.NET as I'm a C# guy.
This is how I would do it in C#, it should be easy to port it to VB.NET


XmlDocument doc = new XmlDocument();
doc.Load(filename);

XmlNode nodeToRemove = null;

foreach(XmlNode node in doc.DocumentElement.ChildNodes)
{
if(node.Name == "JudgeDreadComment")
{
nodeToRemove = node;
break;
}
}

// remove node outside loop
// then we are sure that we don't change the collection we are itterating
over
if(nodeToRemove != null)
{
doc.DocumentElement.RemoveChild(nodeToRemove);
}


Chris
 
W

Wilco Bauwer

Or use XPath, which would look something along these lines:

(C#)
XmlNodeList nodes = doc.SelectNodes("//Photos/JudgeDreddComment");
 
G

Guest

Thanks guys got the anwser.
Used the removechild() to get rid of it.

If anyone is interested in seeing what I cam up with you can find it on my
site in the Code Vault area unde VB.net, XML - How to delete a Node from the
file

Thanks again for your pointers.

Deasun
http://www.tirnaog.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

xml file... 1
Display Links from XML file 1
Update a specific XML node 19
xml transformation convert "<" or ">" value to &lt or &gt 2
Append Node to an XML 2
XML Datasource Help 1
Sort XML 6
Config Error!!! 1

Top