problem formatting xml file using Classic ASP code

I have classic asp code as shown in section (c) below:
It produces test.xml formatted as section (a) below:
How can I change the code to format test.xml as section (b) below:
also, only have one LoginDetails node?
also, where to put vbCrLf to produce new line after node is closed?
Thank you in advance for any help given.

(a) test.xml incorrect formatting in file:
<?xml version="1.0"?>
<LoginDetails><LoginDetails><Provider>sqloledb</Provider><Username>sa</Username><Password>H6web97!</Password><Server>Dinosaur1\sqlexpress</Server><Port>4096</Port><Database>AdminDB2</Database><Table>AdminTable</Table></LoginDetails></LoginDetails>

(b) test.xml with required formatting in file:
<?xml version="1.0"?>
<LoginDetails>
<Provider>sqloledb</Provider>
<Username>sa</Username>
<Password>H6web97!</Password>
<Server>Dinosaur1\sqlexpress</Server>
<Port>4096</Port>
<Database>AdminDB2</Database>
<Table>AdminTable</Table>
</LoginDetails>

(c) classic asp code:
<%
Dim tmpAccountName, xmlpath, blnFileExist, objXMLv, tmpClubID, tmpMemberNo, tmpPassword, xmlObj
xmlpath = server.MapPath("../Template/XML/" & tmpAccountName & "test.xml")
    set objXML = CreateObject("Microsoft.FreeThreadedXMLDOM")
        objXML.async = False
        blnFileExist = objXML.load(xmlpath)
        If not(blnFileExist) Then
            objXML.appendChild(objXML.createProcessingInstruction("xml","version=""1.0""" & vbCrLf))
            objXML.appendChild(objXML.createElement("LoginDetails"))
                Set objXMLv = objXML.createElement("LoginDetails")
                'Assign this node an ID, and give the ID a value
                'objXMLv.setAttribute "ID", tmpClubID
                objXMLv.appendChild(objXML.createElement("Provider"))
                objXMLv.appendChild(objXML.createElement("Username"))
                objXMLv.appendChild(objXML.createElement("Password"))
                objXMLv.appendChild(objXML.createElement("Server"))
                objXMLv.appendChild(objXML.createElement("Port"))
                objXMLv.appendChild(objXML.createElement("Database"))
                objXMLv.appendChild(objXML.createElement("Table"))
                objXMLv.childNodes(0).text = txtProvider
                objXMLv.childNodes(1).text = txtUsername
                objXMLv.childNodes(2).text = txtPassword
                objXMLv.childNodes(3).text = txtServer
                objXMLv.childNodes(4).text = PortTextBox
                objXMLv.childNodes(5).text = DatabaseTextBox
                objXMLv.childNodes(6).text = TableTextBox
                objXML.documentElement.appendChild(objXMLv.cloneNode(true))
                objXML.save(xmlpath)
          End If
    Set objXMLv = nothing
    Set xmlObj = nothing
%>
homeshopperAsked:
Who is Participating?
 
zc2Connect With a Mentor Commented:
you could do that this way:

            Set objXMLv = objXML.createElement("LoginDetails")
            objXML.appendChild(objXMLv)
                set e = objXML.createElement("Provider")
                e.text = txtProvider
                objXMLv.appendChild(e)
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Username")
                e.text = txtUsername
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Password")
                e.text = txtPassword
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Server")
                e.text = txtServer
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Port")
                e.text = PortTextBox
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Database")
                e.text = DatabaseTextBox
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Table")
                e.text = TableTextBox
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))

Open in new window

0
 
homeshopperAuthor Commented:
Thank you for your help, your method works well.
Below is the complete code for others to view:
Case "Write XML"
                                  txtProvider = Session("xtProvider")
              txtUsername = Session("xtUsername")
              txtPassword = Session("xtPassword")
              txtServer = Session("xtServer")
              PortTextBox = Session("xtPortTextBox")
              DatabaseTextBox = Session("xtDatabaseTextBox")
              TableTextBox = Session("xtTableTextBox")
          FileName2 = Session("FileName2")
          xmlpath = server.MapPath(FileName2)
          Dim e, tmpAccountName, xmlpath, blnFileExist, objXMLv, tmpClubID, tmpMemberNo, tmpPassword, xmlObj
          xmlpath = server.MapPath("../Template/XML/" & tmpAccountName & "test.xml")
          set objXML = CreateObject("Microsoft.FreeThreadedXMLDOM")
          objXML.async = False
          blnFileExist = objXML.load(xmlpath)
        If not(blnFileExist) Then
            objXML.appendChild(objXML.createProcessingInstruction("xml","version=""1.0""" & vbCrLf))
            Set objXMLv = objXML.createElement("LoginDetails")
                  objXML.appendChild(objXMLv)
                set e = objXML.createElement("Provider")
                e.text = txtProvider
                objXMLv.appendChild(e)
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Username")
                e.text = txtUsername
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Password")
                e.text = txtPassword
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Server")
                e.text = txtServer
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Port")
                e.text = PortTextBox
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Database")
                e.text = DatabaseTextBox
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
                set e = objXML.createElement("Table")
                e.text = TableTextBox
                objXMLv.appendChild( e )
                objXMLv.appendChild(objXML.createTextNode(vbCrLf))
            objXML.save(xmlpath)
          End If
    Set objXMLv = nothing
    Set xmlObj = nothing
     Case Default
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.