Solved

XMLDOM - Can it modify XML file contents?

Posted on 2007-03-29
10
1,117 Views
Last Modified: 2008-03-03
Greetings,

I came across this excellent Technet Magazine / article and it explains how to parse the sections of an XML file using Microsoft.XMLDOM object.

Hey, Scripting Guy!: Chasing Cars… and XML -- TechNet Magazine, February 2007:
http://www.microsoft.com/technet/technetmag/issues/2007/02/HeyScriptingGuy/default.aspx

Is it possible to modify (or delete a node) an XML file using the Microsoft.XMLDOM object?
0
Comment
Question by:sramesh2k
  • 6
  • 3
10 Comments
 
LVL 34

Author Comment

by:sramesh2k
ID: 18821846
If that's not possible, any idea how to accomplish this using VB 2005?
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18822064
Hi,

Yes it is possible but you would be better off using the System.Xml namespace and loading your XML file into a XmlDocument object and then work on it from there.

Something like this should work:

Import System.Xml


Put this into the form load event of an application:

Dim mdocNewXmlDoc2 As New XmlDocument
Dim oRootNode As XmlNode
Dim oNode As XmlNode

Try

  mdocNewXmlDoc2.Load("C:\test\test.xml")

  oRootNode = mdocNewXmlDoc2.DocumentElement()
  oNode = oRootNode.SelectSingleNode("/Message/NodeToDelete1")

  If Not oNode Is Nothing Then
    oRootNode.RemoveChild(oNode)
  End If

  mdocNewXmlDoc2.Save("C:\test\test.xml")

Catch ex As SystemException
  MessageBox.Show(ex.Message)
End Try


Here is an example of the XMl file used. This is a very simple example

<Message>
  <NodeToDelete1>Hello</NodeToDelete1>
  <NodeToDelete2>Hello</NodeToDelete2>
  <NodeToDelete3>Hello</NodeToDelete3>
  <NodeToDelete4>Hello</NodeToDelete4>
  <NodeToDelete5>Hello</NodeToDelete5>
</Message>

Hope this helps,

Darren
0
 

Assisted Solution

by:paraman_dxb
paraman_dxb earned 175 total points
ID: 18822311
Yes you can,

see the following function it can delete a node given.

Function deleteNode(strXMLFile, strNode)
 Dim objDom
 Dim objRoot
 Dim objNode
 set objXML = Server.CreateObject("Microsoft.XMLDOM")
 objXML.async = false
 objXML.load strXMLFile
 Set objRoot = objXML.documentElement
 Set objNode = objRoot.SelectSingleNode("NodeName='" & strNode& "'")
 objRoot.removeChild(objNode)
 objXML.save strXMLFile
 End Function
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 18822529
Thanks, Darren and paraman_dxb for the codes and explanation. If I have a similar XML file, and if I want to delete the particular node which contains the word "Access", do I need to use the filtering option?

<Data>
  <Script>
    <Category>Microsoft Office</Category>
    <Title>How Can I Print a Microsoft Access Report?</Title>
  </Script>
  <Script>
    <Category>Microsoft Office</Category>
    <Title>How Can I Compact a Microsoft Access Database?</Title>
  </Script>
  <Script>
    <Category>Microsoft Office</Category>
    <Title>How Can I Change an Existing Hyperlink in a Microsoft Word Document?</Title>
  </Script>
  <Script>
    <Category>Enterprise Servers</Category>
    <Title>How Can I Create a Table in a SQL Server Database?</Title>
  </Script>
</Data>
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 18822575
If the Node Title Contains 'Access'  do you want to delete the entire script node or just the title?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 34

Author Comment

by:sramesh2k
ID: 18822579
Darren,

>> do you want to delete the entire script node or just the title

I want to delete the entire "Script" node.
0
 
LVL 18

Accepted Solution

by:
DarrenD earned 325 total points
ID: 18822590
Try this

        Dim mdocNewXmlDoc2 As New XmlDocument
        Dim oNodeList As XmlNodeList
        Dim oRootNode As XmlNode
        Dim oNode As XmlNode

        Try
            mdocNewXmlDoc2.Load("C:\test\test.xml")

            oRootNode = mdocNewXmlDoc2.DocumentElement()
            oNodeList = oRootNode.SelectNodes("/Data/Script")

            For Each oNode In oNodeList
                If oNode.InnerText.Contains("Access") Then
                    oRootNode.RemoveChild(oNode)
                End If
            Next

            mdocNewXmlDoc2.Save("C:\test\test.xml")

        Catch ex As SystemException
            MessageBox.Show(ex.Message)
        End Try
    End Sub
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 18822594
Thanks! Will post back after trying it.
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 18834737
Just installed VB 2005 in my Vista system. Will try it tonight and post back.
0
 
LVL 34

Author Comment

by:sramesh2k
ID: 18837724
Tried Darren's method and it worked a treat.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert MSI to MSM 1 30
How can I assign SimpleXMLElements to variables? 5 31
Linux Scripting 3 59
AWS CLI issues with Tags 3 31
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

760 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

20 Experts available now in Live!

Get 1:1 Help Now