Solved

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

Posted on 2004-10-26
239 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
Question by:rkckjk
    3 Comments
     
    LVL 2

    Accepted Solution

    by:
    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
    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
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Suggested Solutions

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    860 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now