Solved

XMLDOM - Can it modify XML file contents?

Posted on 2007-03-29
10
1,121 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 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
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 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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. 
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

734 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