Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using DOM.. what's the best way to do this

Status
Not open for further replies.

liberty4all

Programmer
Aug 27, 2002
11
0
0
IL
Hi,
I am trying to use XML to display tables on my website, in hope it's faster than using a dataReader or dataSet.
The way I do it is using 3 files:
1. myxsl.xsl - an XSL file that defines how the XML should look like. It is well formed.
2. mysource.aspx - an ASPX file that just selects the data from the database like this:
Code:
void Page_Load(object sender, EventArgs e)
{
SqlConnection dbConnection = new   System.Data.SqlClient.SqlConnection((string)Application["connStr"]);
    
string queryString = @"Select ... for xml raw";
System.Data.SqlClient.SqlCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
dbCommand.CommandType = CommandType.Text;
    
Response.ContentType = "text/xml";
dbConnection.Open();
XmlReader reader = dbCommand.ExecuteXmlReader();
Response.Write(@"<?xml version='1.0' encoding = 'UTF-8'?> <?xml-stylesheet type='text/xsl' href=myxsl.xsl'?><records>");

reader.MoveToContent();
while (!reader.EOF)
      {
            Response.Write(reader.ReadOuterXml());
      }
Response.Write("</records>");
reader.Close();
dbConnection.Close();
reader=null;
dbConnection=null;
}
3. myfile.aspx - an ASPX file that uses an XML data island and has all of the site's banners/menus and so on.
Code:
<script runat="server">
//some C# code...
</script>
<script language="vbscript">
sub window_OnLoad()
set docTbl = CreateObject("MSXML2.DOMDocument.4.0")
docTbl.loadXML dsoTbl.XMLDocument.Xml

set docStyle = CreateObject("MSXML2.DOMDocument.4.0")
docStyle.async=false
docStyle.load "myxsl.xsl"

set docTarget = CreateObject("MSXML2.DOMDocument.4.0")
docTbl.transformNodeToObject docStyle, docTarget


if docTbl.parseError.errorCode <> 0 then
	MsgBox docTable.parseError.reason
else
	display.InnerHTML = docTarget.Xml
end if
end sub
</script>
<html>
 <xml id="dsoTbl" href="myxsl.xsl" src="mysource.aspx">
    </xml>
<head>
<body>
<!--somewhere in the HTML i have this line-->
<div id="display"></div>
</body>
</html>

My problem is that my main page doesn't always load! It could load, and then I'd hit refresh 4-5 times and it would get stuck. That doesn't happen with mysource.aspx, it always load.
Any idea what could cause that? Could the code looking for a parseError be causing it? Maybe I should be going in a different way to accomplish this?

This is the url to the page incase you wanna see it..

Thanks... sorry for the long post
 
It's not a good idea to use vbscript/dom to display the xml/xsl data for compatability/speed.

I would try an xml server control instead or javascript


Have a look here for an example of the xml control.

You can pass your reader to the XmlDocument constructor, or load the string you are writing out as an xml fragment.




MCP, .Net Solutions Development <%_%>
 
Do not use vbscript - it is IE only. Never use any script unless it adds to the user experience or performs a function that can not otherwise be done.

Using XML in this way will not be faster than using a DataReader as you are using a DataReader to get the XML! You should use the appropriate method to display data for the data you are using. In this case, you are getting information from a relational SQL database, so XML is not the best choice.

DataSets are useful for quick and dirty implementations to easily display the data. Personal, I would just bind a DataReader directly to a repeater or datagrid, depending on exactly what you are doing (possibly caching the data). If you r DataSet/DataReader is performing slowly, it is probably because you are retrieving too much data from the database. Try using some form of paging.

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top