Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XMLDOM - Can it modify XML file contents?

Posted on 2007-03-29
10
Medium Priority
?
1,126 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
[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
  • 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 700 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 1300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…

670 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