You are on page 1of 3

using the xml data source object

introduction:
the xml data source object (dso) is a microsoft activex control built into
microsoft internet explorer 4+. using this object, it is possible to extract
content from an external xml file or xml data embedded in the html file into a
html page. in this article, i'll explain how to include the xml dso, extract
content from an external xml file, extract xml data from xml data embedded in the
webpage and manipulation using javascript.

implementation:
initializing an xml-dso object is done using the <object> tag. the classid for the
xml-dso is:
clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39
the above id uniquely identifies the xml-dso. thus we initialize this control in a
webpage as follows:
<object id="someid" classid="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39"></object>
most object's have a number of parameters associated with them but the xml-dso
does not require any parameters as such.

now we can either extract data from an external xml file or using xml data
islands, where we embed xml code in the html page itself. we'll take a look at
these in the examples.

examples:
first, we'll take a look at how to extract data from xml data islands (xml data
included in the html page itself). take a look at the following code:
<!-- example1.htm -->
<html>
<head>
<title>xml dso-example1.htm</title>
</head>
<body bgcolor="#ffffff">

<xml id="xmldb">
<db>
<member>
<name>premshree pillai<name>
<sex>male</sex>
</member>
<member>
<name>vinod</name>
<sex>male</sex>
</member>
</db>
</xml>

<span datasrc="#xmldb" datafld="name"<</span>


<br>
<span datasrc="#xmldb" datafld="sex"></span>

</body>
</html>
the output of the above is:
premshree pillaimale

note that, in the code for example1.htm, we have not initialised any xml-dso
object. thus, when you use a xml data island, the object is implicitly created.

in the above code, we have included a xml data island using the <xml> tag. we have
assigned it an id, xmldb for use later. now, we can extract data from the xml data
island using the html tags like <a>, <span>, <div> etc. as you can see, we have
extracted data using the <span> tag. note the attributes, datasrc and datafld in
the <span> tag. datasrc is used to specify the id of the data island you want to
extract data from. datafld is used to specify the xml tag you want the data from
(here, name in first <span> and sex in second <span>).

note that we have two <name> and <sex> tags in our xml data island, but using the
above method we can extract only the first instances of these tags. to extract all
instances, this can be done using the <table> tag. take a look at the following
example:
<!-- example2.htm -->
<html>
<head>
<title>xml dso-example2.htm</title>
</head>
<body bgcolor="#ffffff">

<xml id="xmldb">
<db>
<member>
<name>premshree pillai<name>
<sex>male</sex>
</member>
<member>
<name>vinod</name>
<sex>male</sex>
</member>
</db>
</xml>

<table datasrc="#xmldb" border="1">


<thead>
<th>name</th>
<th>sex</th>
</thead>
<tr>
<td><div datafld="name"></div></td>
<td><div datafld="sex"></div></td>
</tr>
</table>

</body>
</html>

the output of the above is:

name sex
premshree male
pillai vinod male
----------------------------------------------------------------------------------
-------------
http://www.devx.com/xml/article/15497

<object id="someid" classid="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39"></object>

-------------------------------
how to bind a gridview control to xml in asp.net
in this example, your xml content is assumed to be ready and well formatted. to be
compatible with a gridview, the xml document has to have a database-like format
(table and records):

<countries>
<country>
<name>angola</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>benin</name><code>204</code><size>435 amp</size>
</country>
</countries>

drag a gridview component from the toolbox.


add to your gridview a prerender event.
write your code (see below) to bind a dataset to the gridview.

public partial class _default : system.web.ui.page


{
protected void mygridview_prerender(object sender, eventargs e)
{
// creates a dataset and loads it with an xml content
dataset adataset = new dataset();
adataset.readxml(new stringreader(axmldoc.outerxml));

// bind the dataset to the grid view


gridview gv = (gridview)sender;
gv.datasource = adataset;
gv.databind();
}
}

You might also like