Using XML DOM to update machine.config

H

Highlander

Hello all.
The following lines are an excerpt from a machine.config file:

<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="10"/>
</connectionManagement>
</system.net>

<system.web>
<processModel
enable="true"
timeout="Infinite"
idleTimeout="Infinite"
shutdownTimeout="0:00:05"
requestLimit="Infinite"
requestQueueLimit="5000"
restartQueueLimit="10"
memoryLimit="60"
webGarden="false"
cpuMask="0xffffffff"
userName="machine"
password="AutoGenerate"
logLevel="Errors"
clientConnectedCheck="0:00:05"
comAuthenticationLevel="Connect"
comImpersonationLevel="Impersonate"
responseDeadlockInterval="00:03:00"
maxWorkerThreads="100"
maxIoThreads="100"
minWorkerThreads="50"
/>
</system.web>
</configuration>

I'm using the following VBScript to edit the machine.config file and
change the "maxconnection" value from 10 to 48:

Dim strServer, XMLFilePath, XMLFileName, XMLFile, XMLFileBak
strServer = "000-WEB15-CRS"
XMLFilePath = "\\" & strServer & _
"\C$\Winnt\Microsoft.NET\Framework\v1.1.4322\CONFIG\"
XMLFileName = "machine.config"
XMLFile = XMLFilePath & XMLFileName
XMLFileBak = XMLFileName & "_Backup_" & ".txt"

Set xmlDoc = CreateObject("Msxml.DOMDocument")
xmlDoc.async = False
xmlDoc.PreserveWhitespace = True
xmlDoc.load XMLFile

'~~~ Create backup of original XML file
Set objFSO = CreateObject("Scripting.FileSystemObject")
CreateObject("Scripting.FilesystemObject") _
.GetFile(XMLFile).Name = XMLFileBak

Dim strNode, strKeyName, strNewValue
strNode = "/*/*/*/*[@address=" & Chr(34) & "*" & Chr(34) & "]"
strKeyName = "maxconnection"
strNewValue = "48"

Set objNode = xmlDoc.selectSingleNode (strNode)
objNode.setAttribute strKeyName, strNewValue
xmlDoc.Save XMLFile

Set xmlDoc = nothing
Set objFSO = nothing
Set objNode = nothing

The script works fine, except for one thing. The whitespace appears to
be preserved throughout the machine.config file - except for the
"<processModel" section. This section gets changed - the entire section
ends up on one horizontal line:

<processModel enable="true" timeout="Infinite"
idleTimeout="Infinite" shutdownTimeout="0:00:05"
requestLimit="Infinite" requestQueueLimit="5000" restartQueueLimit="10"
memoryLimit="60" webGarden="false" cpuMask="0xffffffff"
userName="machine" password="AutoGenerate" logLevel="Errors"
clientConnectedCheck="0:00:05" comAuthenticationLevel="Connect"
comImpersonationLevel="Impersonate" responseDeadlockInterval="00:03:00"
maxWorkerThreads="100" maxIoThreads="100" minWorkerThreads="50"/>

Any idea what's causing this? Any help would be greatly appreciated.
Thanks!

- Dave
 
M

mr_unreliable

Highlander, if you want your generated xml to be tidy,
then you are going to have to tidy it up yourself.

There are a number of "tidy-up-xml" mini-apps out there.
I wrote one myself in about an hour.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
 
J

Joseph Kesselman

Can't post to the Microsoft newsgroups, so this answer may not help
Highlander unless s/he is reading c.t.x...

Whitespace between XML attributes is not meaningful, and most XML tools
don't retain it or attempt to prettify it. You could try finding a
serializer which does so and plug that into your processing, or you
could write your own (warning: doing this right is nontrivial; you
should probably look at adapting one rather than trying to do it from
scratch), or you could postprocess the document to prettify the
attribute indentation -- or you could simply not worry about it, since
the change is harmless unless some other tool is badly broken.
 
H

Highlander

mr_unreliable said:
Highlander, if you want your generated xml to be tidy,
then you are going to have to tidy it up yourself.

There are a number of "tidy-up-xml" mini-apps out there.
I wrote one myself in about an hour.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)

jw - Can you post the code for the "tidy-up-xml" mini-app that you
wrote? Or point me in the direction of where I can find the other
mini-apps that are out there? Thanks!

- Dave
 
M

mr_unreliable

--- <snipper> ---
' retrieve the (untidy) xml from the dom...
Dim sXML ' as string
sXML = xmlDoc.xml
' MsgBox(sXML)

' ----------------------------------------------
' tidy up the result...
' ----------------------------------------------
Dim sTidyXML ' as string
' (first step) tidy the result...
sTidyXML = Replace(sXML, ">", ">" & vbCrLf)
' (second step)
sTidyXML = Replace(sTidyXML, "<", " <")
' MsgBox(sTidyXML)
' (third step) remove indent from rootnode tags...
sTidyXML = Replace(sTidyXML, " <?", "<?")
sTidyXML = Replace(sTidyXML, vbCrLf & vbCrLf, vbCrLf)
sTidyXML = Replace(sTidyXML, " <" & sRootNodeName, "<" &
sRootNodeName)
sTidyXML = Replace(sTidyXML, " </" & sRootNodeName, "</" &
sRootNodeName)
MsgBox(sTidyXML)

' sXMLTidySpec = GetLocalDirectory() & sXMLTidyFile ' for debugging
sXMLTidySpec = fso.GetSpecialFolder(tempFolder) & "\" & sXMLTidyFile
' construct the output file...
dbPrint sMe & "save xml tidy file: " & sXMLTidySpec

' write out the tidy file...
Dim oFile, oTextStream ' as object(s)
fso.CreateTextFile sXMLTidySpec, allowOverwrite ' creates the file
(no return)...
Set oXMLOutFile = fso.GetFile(sXMLTidySpec) ' "objectify" the file...

Set oTextStream = oXMLOutFile.OpenAsTextStream(ForWriting) ' ,
TristateUseDefault)
oTextStream.Write sTidyXML
oTextStream.Close

--- <end snippet> ---

Crude but effective, jw
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top