PRB: Element Is Not Updated When You Add Data to an XML Data Island (303866)



The information in this article applies to:

  • Microsoft Internet Explorer (Programming) 5
  • Microsoft Internet Explorer (Programming) 5.01
  • Microsoft Internet Explorer (Programming) 5.01 SP1
  • Microsoft Internet Explorer (Programming) 5.5

This article was previously published under Q303866

SYMPTOMS

When you bind an element to XML data and dynamically add more data to the data island through script, the Data Source Object (DSO) is not updated. As a result, the element cannot display the newly added data.

This problem is not specific to the XML data island; it applies to other data sources as well.

CAUSE

This problem occurs because the DSO determines the schema of the data when the data binding agent first invokes it. Even if you add more data to the source later, the DSO cannot change the schema. Therefore, the element that is binding to the DSO fails to display the updated data.

RESOLUTION

To resolve this problem, follow these steps:
  1. Call loadXML on the XML DSO with the updated data island to reload the XML DSO.
  2. Add an empty element statically to allow Internet Explorer to construct the schema.
  3. Remove the element programmatically when it loads. After you do so, the schema builder sees the element, and you can dynamically add attributes and text to the element later without any problem.

MORE INFORMATION

Steps to Reproduce Behavior

The following example binds a table to an XML data island and dynamically adds a new element named "bio" to the data island through script. However, when the data binding agent first invokes the DSO, no "bio" nodes exist in the tree. Thus, the DSO does not know about the "bio" element, and the table is not updated even after you rebind it to the DSO.
<HTML>
<HEAD>
<TITLE>Run-time binding with XML data island</TITLE>
<script>

function bind1()
{
	  tbl1.dataSrc = "";
	  doc = xml1.XMLDocument;
	  curNode = doc.selectSingleNode("/composers/composer1");
	  txtNode = doc.createTextNode("French composer of piano music");
	  elem = doc.createElement("bio");
	  elem.appendChild(txtNode);
	  curNode.appendChild(elem);
	  
	  // This shows that the data island has been updated with the 
	  // new data.
	  alert(xml1.xml);

	  // The following line reloads the data from the XML data island
	  // into the DSO. Uncomment this line for the code to work properly.
	  // xml1.loadXML(xml1.xml);
	  
	  tbl1.dataSrc = "#xml1";  
}
</script>


</HEAD>
<BODY>

<XML ID="xml1">
<composers>
	<composer1>
	    <firstname>Claude</firstname>
	    <lastname>Debussy</lastname>
	    <origin>France</origin>
	</composer1>
</composers>
</XML>

<p>
<div style="font:10 pt Verdana"> 
The XML data island that is the datsrc of this table does not 
have an element named "bio". Clicking Update dynamically adds that 
element to the data island and rebinds the table:
</div>
</p>


<p>
<table id=tbl1 datasrc="#xml1" border=1>
	<tr width="100%" style="background-color:'#ffe4b5'; font:'12pt Arial'">
		<td width="20%">FirstName</td>
		<td width="20%">LastName</td>
		<td width="20%">Origin</td>
		<td width="20%">Bio</td>
	</tr>
	<tr>
		<td><span datafld="firstname"></td>
		<td><span datafld="lastname"></td>
		<td><span datafld="origin"></td>
		<td><span datafld="bio"></td>
	</tr>
</table>
</p>
<button onClick="bind1();" id=button1 name=button1>Update</button>
</BODY>
</HTML>
				

REFERENCES

For more information, see the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:5/12/2003
Keywords:kbprb KB303866