sramesh2k
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?
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?
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:\te st\test.xm l")
oRootNode = mdocNewXmlDoc2.DocumentEle ment()
oNode = oRootNode.SelectSingleNode ("/Message /NodeToDel ete1")
If Not oNode Is Nothing Then
oRootNode.RemoveChild(oNod e)
End If
mdocNewXmlDoc2.Save("C:\te st\test.xm l")
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</Node ToDelete1>
<NodeToDelete2>Hello</Node ToDelete2>
<NodeToDelete3>Hello</Node ToDelete3>
<NodeToDelete4>Hello</Node ToDelete4>
<NodeToDelete5>Hello</Node ToDelete5>
</Message>
Hope this helps,
Darren
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:\te
oRootNode = mdocNewXmlDoc2.DocumentEle
oNode = oRootNode.SelectSingleNode
If Not oNode Is Nothing Then
oRootNode.RemoveChild(oNod
End If
mdocNewXmlDoc2.Save("C:\te
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</Node
<NodeToDelete2>Hello</Node
<NodeToDelete3>Hello</Node
<NodeToDelete4>Hello</Node
<NodeToDelete5>Hello</Node
</Message>
Hope this helps,
Darren
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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>
<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?
ASKER
Darren,
>> do you want to delete the entire script node or just the title
I want to delete the entire "Script" node.
>> do you want to delete the entire script node or just the title
I want to delete the entire "Script" node.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! Will post back after trying it.
ASKER
Just installed VB 2005 in my Vista system. Will try it tonight and post back.
ASKER
Tried Darren's method and it worked a treat.
ASKER