I've been struggling with this method, in what seems fairly straightforward.
Dim objXML, objNode, objDeleted, objRoot
Set objXML = CreateObject("MSXML2.DOMDocument.4.0")
Set objNode = CreateObject("MSXML2.DOMDocument.4.0")
objXML.async = false
objXML.Load("C:\Config.xml")
Set objNode = objXML.documentElement.selectSingleNode("/Servers/Server/Modules/Module/ModuleParameter[ServiceName='OldService']")
objXML.documentElement.removeChild(objNode)
objXML.Save ("C:\Config.xml")
Set objXML = nothing
Set objRoot = nothing
Set objDeleted = nothing
Set objNode = nothing
That gives me an error "Invalid procedure call or argument" for the line:
objXML.documentElement.removeChild(objNode)
If I replace the middle section with:
Set objRoot = objXML.documentElement
Set objNode = objXML.docu开发者_如何转开发mentElement.selectSingleNode("/Servers/Server/Modules/Module/ModuleParameter[ServiceName='OldService']")
objRoot.removeChild(objNode)
It says the same thing. Finally, if I just try:
Set objNode = objXML.documentElement.selectSingleNode("/Servers/Server/Modules/Module/ModuleParameter[ServiceName='OldService']")
objNode.parentNode.removeChild(objNode)
It tells me "object required". I've searched the web, and all the examples I'm seeing use variations on these three pieces of code, and they supposedly work. What am I missing here? Thanks for any help!
I finally figured it out. It was actually the line:
Set objNode = objXML.documentElement.selectSingleNode("/Servers/Server/Modules/Module/ModuleParameter[ServiceName='OldService']")
That was causing the problem. I was missing one level in the tree, and also had to add another specifier to get exactly what I wanted. Thus:
Set objNode = objXML.documentElement.selectSingleNode("//Servers/Server/Modules/Module[ModuleName='Services']/ModuleParameters/ModuleParameter[ServiceName='OldService']")
Worked fine.
精彩评论