Saving XML

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Developer's Guide

Saving XML

You can save XML on the server using the save method, as shown in the following sample ASP file.

SaveXmlString.asp

<HTML>
<BODY>
<!-- SaveXmlString.asp -->
<%
   If IsEmpty(Request("strXml")) Then
      Msg = "Please enter a brief XML string, such as '&lt;root/&gt;'."
   Else
      Dim str, xmldoc
      Set xmldoc = CreateObject("Msxml2.DOMDocument.5.0")
      xmldoc.loadXML Request("strXml")
      If xmlDoc.parseError.errorCode <> 0 Then
          Msg = "The string you entered was not well-formed XML. " & _
                xmlDoc.parseError.reason
      Else
          xmldoc.save(Server.MapPath("saved.xml"))
          str = Request("strXml")
          str = Replace(str, Chr(38),"&#38;", 1, -1, 1)
          str = Replace(str, Chr(34),"&#34;", 1, -1, 1)
          str = Replace(str, Chr(46),"&#46;", 1, -1, 1)
          str = Replace(str, Chr(60),"&lt;", 1, -1, 1)
          str = Replace(str, Chr(62),"&gt;", 1, -1, 1)
          Msg = "<P>Your XML string:<BR>" & str & _
                "<BR>has been saved to the server as <B>saved.xml</B>.</P>"
     End If
   End If
%>
<FORM METHOD="POST" ACTION="SaveXmlString.asp">
<PRE>
<%= Msg %><BR>
<INPUT TYPE="TEXT" NAME="strXml" SIZE=75
VALUE="<%= Request("strXml")%>"><BR>
<INPUT TYPE="Submit" VALUE="Submit">
</PRE>
</FORM>
</BODY>
</HTML>

Try It!

  1. On your server computer where you are running Internet Information Services (IIS), open Notepad.
  2. Copy SaveXmlString.asp from above. Paste it into the Notepad window.
  3. From the File menu, click Save As. Save the file as SaveXmlString.asp to a folder (such as C:\test).
  4. From the Start menu, click Run. Type "inetmgr" to open the Internet Information Services snap-in.
  5. From IIS, navigate to Internet Information Services\ <your_Web_server_computer>\Default Web Site.
  6. From the Action menu, point to New, then click Virtual directory.
  7. Complete the New Virtual Directory wizard using the following information:
    • For Alias, enter the name "test" for your new virtual directory.
    • For Directory, browse and select the path (such as "C:\test") that you used in step 3.
    • For Access Permissions, be sure to select the Write check box as well as accepting the default permissions (Read, Run scripts).
  8. Using Internet Explorer, open the Web URL (such as "http://MyServer/test/SaveXmlString.asp") to load the page.
  9. Enter a brief well-formed XML string (such as "<root/>") in the form. Click Submit.
Note   The MapPath method resolves relative paths into the full paths required on the server.

Output

If you enter the suggested XML string ("<root/>"), you should see the following appear in the browser:

Your XML string:
<root/>
has been saved to the server as saved.xml.

You can then open the directory you used in steps 3 and 7 and verify that saved.xml is present.