add reference to microsoft xml
Public Sub CreateXML()
Dim objDom As DOMDocument
Dim objXMLRootelement As IXMLDOMElement
Dim objXMLelement As IXMLDOMElement
Dim objXMLChildElement As IXMLDOMElement
Dim objXMLattr As IXMLDOMAttribute
Set objDom = New DOMDocument
'~~> Creates root element
Set objXMLRootelement = objDom.createElement("Entry")
objDom.appendChild objXMLRootelement
'~~> Create Time element
Set objXMLelement = objDom.createElement("Country")
objXMLelement.Text = "Sri Lanka"
objXMLRootelement.appendChild objXMLelement
'~~> Create Date element
Set objXMLelement = objDom.createElement("Date")
objXMLRootelement.appendChild objXMLelement
'~~> Creates Attribute to the Date Element and set value
Set objXMLattr = objDom.createAttribute("Value")
objXMLattr.NodeValue = "3/2/2012"
objXMLelement.setAttributeNode objXMLattr
'~~> Create Time element
Set objXMLelement = objDom.createElement("Time")
objXMLRootelement.appendChild objXMLelement
'~~> add a child element to an element
Set objXMLChildElement = objDom.createElement("Local")
objXMLChildElement.Text = "9:30"
objXMLelement.appendChild objXMLChildElement
'~~> Saves XML data to a file
objDom.Save ("C:\CSharpNoob2011.xml")
Debug.Print objDom.XML
End sub
Thank you for good example! Exactly what I looked for.
ReplyDeleteSet objDom = New DOMDocument
ReplyDeleteerror received at this line...
Error:activeX component can't create object
This comment has been removed by the author.
ReplyDelete