Solved

XMLDOM - Can it modify XML file contents?

Posted on 2007-03-29
10
1,119 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the selected ValueMember of Combobox 5 30
Assigning handler to UserControls in flowlayoutpanel 4 22
get row value in vb.net 4 14
Write to a printer using vb.net 9 26
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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