Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to persist server-side XML in a client-side, hidden HTML element


View products that this article applies to.

This article was previously published under Q299388

↑ Back to the top


Summary

To persist server-side XML in client-side, hidden HTML elements and access it in client-side script, you can use a combination of server-side Active Server Pages (ASP) and client-side scripts. This article includes a code sample that demonstrates how to implement this process.

↑ Back to the top


More information

It is a common programming requirement to persist server-side XML in client-side HTML elements so that the XML is accessible to client-side script. One method is to use the XML Data Source object (DSO) in the client-side HTML to reference and store the XML data. However, this method does not provide a cross-browser solution because the XML DSO is an Internet Explorer-specific object.

Another method is to use standard HTML in an <INPUT TYPE=HIDDEN> HTML element to persist the XML. You can then use the Value property of the hidden HTML element in client-side script to access the XML that is persisted in it. Then, the client-side script can run custom code to manipulate the XML (for example, load the XML into an instance of an XML Document Object Model object on the client, and persist the XML to disk).

Step-by-step example

If a newer version of MSXML has been installed in Side-by-Side mode, then to run the sample code with that specific version, you must explicitly use the GUIDs or ProgIDs for that version. For example, MSXML version 4 only installs in side-by-side mode. Please refer to the following article in the Microsoft Knowledge Base to see what code changes required to run the sample code with the MSXML 4.0 parser:
305019� MSXML 4.0 specific GUIDs and ProgIDs
For example, in the code below, you would create the objects using:
Set clientSideXMLDoc = CreateObject("MSXML2.DomDocument.4.0")
and
Set serverSideXMLDoc = Server.CreateObject("MSXML2.DomDocument.4.0")
  1. In Notepad, create a new XML document named Books.xml, and paste the following, well-formed XML:
    <?xml version="1.0"?>
     <Books> 
        <Book QTY="10">
            <Title>Beginning XML</Title>       
        </Book>
        <Book QTY="2">
           <Title>Mastering XML</Title>       
       </Book>
    </Books>
    					
  2. Save Books.xml in one of your Microsoft Internet Information Server (IIS) virtual directories.
  3. In Notepad, create a new ASP page named LoadHiddenXML.asp, and paste the following code:
    <HTML>
    
    <HEAD>
       <SCRIPT Language=VBScript>
        Sub btnXML_onClick()
           Dim clientSideXMLDoc
           Set clientSideXMLDoc = CreateObject("Microsoft.XMLDOM")
           clientSideXMLDoc.loadxml form1.txtXML.value
           Msgbox clientSideXMLDoc.xml 
        End Sub 
       </SCRIPT>
    </HEAD>
    
    <BODY>
    <%
         Dim serverSideXMLDoc
         Set serverSideXMLDoc = Server.CreateObject("Microsoft.XMLDOM")
         serverSideXMLDoc.load Server.MapPath("books.xml")   
    %>
    
    
    <FORM id="form1" method="POST" action="hiddenxml.asp"> 
     <INPUT ID='txtXML' name='txtXML' TYPE='hidden' VALUE='<% =serverSideXMLDoc.xml %>'> 
     <INPUT TYPE=Button id="btnXML" value="Display Hidden XML">  
    </FORM>
    
    </BODY>
    </HTML>
    					
  4. Save LoadHiddenXML.asp in the same IIS virtual directory where you saved Books.xml.
  5. The ASP code contains server-side script to load the contents of Books.xml into an instance of the Microsoft XML (MSXML) Document Object Model (DOM). The loaded XML is then assigned as the value of the hidden HTML element named txtXML.

    Open Internet Explorer, and browse to LoadHiddenXML.asp. When the page is displayed, click Display Hidden XML to load and display the XML (using a client-side MSXML DOM object) that the server-side ASP script assigns to the txtXML hidden element.

↑ Back to the top


Keywords: KB299388, kbhowto

↑ Back to the top

Article Info
Article ID : 299388
Revision : 5
Created on : 7/10/2006
Published on : 7/10/2006
Exists online : False
Views : 452