Solved

XMLDOM - Can it modify XML file contents?

Posted on 2007-03-29
10
1,122 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

627 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