[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 120
  • Last Modified:

Help with removing data element

Hi,

How do you remove a data element from a xml file using VB.NET?

Thanks,

Victor
0
vcharles
Asked:
vcharles
1 Solution
 
Randy PooleCommented:
Dim doc As New XmlDocument()
doc.Load("file.xml")
Dim n As XmlNode = doc.SelectSingleNode("xpath expression")
If n IsNot Nothing Then
  n.ParentNode.RemoveChild(n)
  doc.Save("file.xml")
End If
set doc=nothing

Open in new window

replace "file.xml" with the name of the file you want to alter, and change the "xpath expression" to select the node you would like to remove
0
 
Fernando SotoCommented:
Hi Victor;

You did not specify whether you want to remove a single node or multiple nodes with the same node name. In the below code snippet lets say you have multiple nodes with the name Item and wish to remove them all just change NODENAME in the code with Item and it should do it for you. The code can be easily modify to remove a single node.

Dim inputXML As String = "File path and name"
Dim saveXML As String = "File path and name"
'' Load the XML document
Dim xdoc = XDocument.Load(inputXML)

'' Replace the "NODENAME"
Dim results As List(Of XElement) = (From n In xdoc.Descendants("NODENAME") _
                                    Select n).ToList()
                              
For Each node As XElement In results 
    node.Remove()
Next

xdoc.Save(saveXML)

Open in new window

0
 
vcharlesAuthor Commented:
Thank You.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now