Link to home
Start Free TrialLog in
Avatar of sramesh2k
sramesh2kFlag for India

asked on

XMLDOM - Can it modify XML file contents?

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?
Avatar of sramesh2k
sramesh2k
Flag of India image

ASKER

If that's not possible, any idea how to accomplish this using VB 2005?
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
SOLUTION
Avatar of paraman_dxb
paraman_dxb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>
If the Node Title Contains 'Access'  do you want to delete the entire script node or just the title?
Darren,

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

I want to delete the entire "Script" node.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks! Will post back after trying it.
Just installed VB 2005 in my Vista system. Will try it tonight and post back.
Tried Darren's method and it worked a treat.