vb 写XML时的层次感问题 --- 希望大侠指点
问题:vbs 写XML后希望xml文件有层次感
写入xml的效果如下:<Environment><Variable><Name>username</Name><Value>C000012</Value></Variable></Environment>
希望的效果:
<Environment>
<Variable>
<Name>username</Name>
<Value>C000012</Value>
</Variable>
</Environment>
代码如下:
Public Sub CreateEnvVar(Vname,Vvalue,filepath)
Set objxml= CreateObject("MSXML.DOMDocument")
objxml.load filepath
Set EnvNode = objxml.createElement("Environment")
objxml.appendChild(EnvNode)
objxml.save filepath
objxml.load filepath
Set Env = objxml.getElementsByTagName("Environment")(0)
length = Env.childNodes.length
If length = 0 Then
Set NewNode = objxml.createElement("Variable")
Set NameNode = objxml.createElement("Name")
NameNode.nodetypedvalue = Vname
Set ValueNode = objxml.createElement("Value")
ValueNode.nodetypedvalue = Vvalue
NewNode.appendChild(NameNode)
NewNode.appendChild(ValueNode)
Env.appendChild(NewNode)
End If
objxml.save filepath
End Sub