Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add an element and remove an element in a XML file?

Posted on 2004-10-26
3
Medium Priority
?
243 Views
Last Modified: 2010-04-23
Basically, I would like to know how to add or remove an elemet from the XML file below using VB .Net:

<?xml version="1.0" standalone="yes"?>
<RECORD>
  <TYPE>
    <TIME_STARTED>4:44</TIME_STARTED>
     <TIME_ENDED>6:33</TIME_ENDED>
</TYPE>
</RECORD>

When the 'TODAY' element is added the XML file would look like this:

<?xml version="1.0" standalone="yes"?>
<RECORD>
  <TYPE>
    <TODAY>Oct. 26 2004</TODAY>
    <TIME_STARTED>4:44</TIME_STARTED>
     <TIME_ENDED>6:33</TIME_ENDED>
</TYPE>
</RECORD>

And when the "TODAY' element is removed it would look like the first XML file in this question.

Thanks

0
Comment
Question by:Richard Kreidl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
UncleMidriff earned 1000 total points
ID: 12414250
This code will do what you need, I think.  Let me know if I missed something or the point completely ;)  .
_____________________________________________________________________________
        Dim Path As String = Application.StartupPath & "\XMLTest.xml"
        Dim XMLDoc As New Xml.XmlDocument
        Dim RecordNode As Xml.XmlNode
        Dim TodayNode As Xml.XmlNode
        Dim i As Integer

        XMLDoc.Load(Path)

        RecordNode = XMLDoc.ChildNodes(1)

        'remove today nodes
        For i = 0 To RecordNode.ChildNodes.Count - 1
            TodayNode = RecordNode.ChildNodes(i).Item("TODAY")
            RecordNode.ChildNodes(i).RemoveChild(TodayNode)
        Next

        'add today nodes
        For i = 0 To RecordNode.ChildNodes.Count - 1
            TodayNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "TODAY", "")
            TodayNode.InnerText = CStr(Now())
            RecordNode.ChildNodes(i).PrependChild(TodayNode)
        Next

        XMLDoc.Save(Application.StartupPath & "\XMLTestResultingFile.xml")
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12414258
the easiest way is to save xml file to dataset and then add/remove records in dataset. After that save ds to xml
0
 
LVL 2

Expert Comment

by:UncleMidriff
ID: 12414350
That's a good idea as well.  But if he did that and removed the column corresponding to "TODAY," would it remove every <TODAY></TODAY> element in the XML file, or would he be able to specify to leave some of the <TODAY></TODAY> elements and delete others?
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question