Accessing XML file using JavaScript And ASP.net |VB code
Posted
by Bubba
on Stack Overflow
See other posts from Stack Overflow
or by Bubba
Published on 2009-12-08T11:55:32Z
Indexed on
2010/06/01
16:03 UTC
Read the original article
Hit count: 318
Am trying to read in data from an xml file but using javascript which is embedded into my asp.net|vb code. I am new to asp.net but coming from a programming background.
- so I declared the xml objects for the appropriate browsers, as well as the name of the local xml to read data from,
- I then start by appending the create the table tag and then append it to the div tag in hack5.aspx
- I declare the variable that will represent/ hold the xml returned data object.
- I then run a for loop , before creating a row tag and then appending it to the div tag in hack5.aspx
- I then create the a row tag and then appending it to the div tag in hack5.aspx
- | then create a TextNode which is passed to variable, then create a td and append to div . then lastly append the textnode to td
this format is the same for creating another 13 td tags that are to hold the data.
The main problem is when I run the script - I see nothing display on my screen . no errors are shown, but with your sample code runs smoothly.
So the first file hack5.aspx is as follows:
<%@ Page Language="VB"
AutoEventWireup="false"
CodeFile="hack5.aspx.vb"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Diplaying MessageBox from ASP.NET</title> </head> <body>
<form id="form1" runat="server">
<div id="showtime" >
</div>
</form> </body> </html>
The next file hack5.aspx.vb is as follows:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim scriptString as String = "<script language=JavaScript> if (window.XMLHttpRequest) "
scriptString += " { "
scriptString += " xhttp=new XMLHttpRequest(); "
scriptString += " } "
scriptString += " else "
scriptString += " { "
scriptString += " xhttp=new ActiveXObject('Microsoft.XMLHTTP'); "
scriptString += " } "
scriptString += " xhttp.open('GET','yes.xml',false); "
scriptString += " xhttp.send(null);"
scriptString += " xmlDoc= xhttp.responseXML; "
scriptString += " var table1 = document.createElement('table'); "
scriptString += " document.getElementById('showtime').appendChild(table1); "
scriptString += " var x=xmlDoc.getElementsByTagName('Table'); "
scriptString += " for (i=0;i<x.length;i++) "
scriptString += " { "
scriptString += " var assessment = document.createTextNode(x[i].getElementsByTagName('Assessment')[0].childNodes[0].nodeValue);"
scriptString += " var row1 = document.createElement('tr'); "
scriptString += " document.getElementById('showtime').appendChild(row1); "
scriptString += " var column1 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column1); "
scriptString += " column1.appendChild(assessment); "
scriptString += " var Issue_Date = document.createTextNode(x[i].getElementsByTagName('Issue_Date')[0].childNodes[0].nodeValue);"
scriptString += " var column2 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column2); "
scriptString += " column2.appendChild(Issue_Date); "
scriptString += " var Due_Date = document.createTextNode(x[i].getElementsByTagName('Due_Date')[0].childNodes[0].nodeValue);"
scriptString += " var column3 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column3); "
scriptString += " column3.appendChild(Due_Date); "
scriptString += " var Interest = document.createTextNode(x[i].getElementsByTagName('Interest')[0].childNodes[0].nodeValue);"
scriptString += " var column4 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column4); "
scriptString += " column4.appendChild(Interest); "
scriptString += " var Summary = document.createTextNode(x[i].getElementsByTagName('Summary')[0].childNodes[0].nodeValue);"
scriptString += " var column5 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column5); "
scriptString += " column5.appendChild(Summary);"
scriptString += " var Amount_Due= document.createTextNode(x[i].getElementsByTagName('Amount_Due')[0].childNodes[0].nodeValue);"
scriptString += " var column6 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column6); "
scriptString += " column6.appendChild(Amount_Due);"
scriptString += " var IEduty = document.createTextNode(x[i].getElementsByTagName('IEduty')[0].childNodes[0].nodeValue);"
scriptString += " var column7 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column7); "
scriptString += " column7.appendChild(IEduty);"
scriptString += " var LEsurtax = document.createTextNode(x[i].getElementsByTagName('LEsurtax')[0].childNodes[0].nodeValue);"
scriptString += " var column8 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column8); "
scriptString += " column8.appendChild(LEsurtax);"
scriptString += " var CEsurtax = document.createTextNode(x[i].getElementsByTagName('CEsurtax')[0].childNodes[0].nodeValue);"
scriptString += " var column9 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column9); "
scriptString += " column9.appendChild(CEsurtax);"
scriptString += " var EXduty = document.createTextNode(x[i].getElementsByTagName('EXduty')[0].childNodes[0].nodeValue);"
scriptString += " var column10 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column10); "
scriptString += " column10.appendChild(EXduty);"
scriptString += " var IMvat = document.createTextNode(x[i].getElementsByTagName('IMvat')[0].childNodes[0].nodeValue);"
scriptString += " var column11 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column11); "
scriptString += " column11.appendChild(IMvat);"
scriptString += " var SYSfee = document.createTextNode(x[i].getElementsByTagName('SYSfee')[0].childNodes[0].nodeValue);"
scriptString += " var column12 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column12); "
scriptString += " column12.appendChild(SYSfee);"
scriptString += " var AItax = document.createTextNode(x[i].getElementsByTagName('AItax')[0].childNodes[0].nodeValue);"
scriptString += " var column13 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column13); "
scriptString += " column13.appendChild(AItax);"
scriptString += " var Cduty = document.createTextNode(x[i].getElementsByTagName('Cduty')[0].childNodes[0].nodeValue);"
scriptString += " var column14 = document.createElement('td'); "
scriptString += " document.getElementById('showtime').appendChild(column14); "
scriptString += " column14.appendChild(Cduty);"
scriptString += " } "
scriptString += " <"
scriptString += "/"
scriptString += "script>"
If(Not ClientScript.IsStartupScriptRegistered("clientScript"))
ClientScript.RegisterClientScriptBlock(Me.GetType(),"clientScript", scriptString)
End If
End Sub
End Class
And finally the xml file is as follows:
<?xml version="1.0" encoding="utf-8"
?> <DataSet
xmlns="http://tempuri.org/">
<xs:schema id="NewDataSet" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet"
msdata:IsDataSet="true"
msdata:UseCurrentLocale="true">
<xs:complexType> <xs:choice
minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType> <xs:sequence>
<xs:element name="UserName"
type="xs:string" minOccurs="0" />
<xs:element name="Password"
type="xs:string" minOccurs="0" />
<xs:element name="UserLevel"
type="xs:string" minOccurs="0" />
<xs:element name="FName"
type="xs:string" minOccurs="0" />
<xs:element name="LName"
type="xs:string" minOccurs="0" />
<xs:element name="Branch"
type="xs:string" minOccurs="0" />
<xs:element name="Department"
type="xs:string" minOccurs="0" />
</xs:sequence> </xs:complexType>
</xs:element> </xs:choice>
</xs:complexType> </xs:element>
</xs:schema> <diffgr:diffgram
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns=""> <Table
diffgr:id="Table1"
msdata:rowOrder="0">
<Assessment>CHR/A157/2009</Assessment>
<Issue_Date>20/10/2009</Issue_Date>
<Due_Date>01/11/2009</Due_Date>
<Interest>2.00</Interest>
<Summary>BENTLEY 2009</Summary>
<Amount_Due>28000000.00</Amount_Due>
<IEduty>3000000.00</IEduty>
<LEsurtax>4000000.00</LEsurtax>
<CEsurtax>5000000.00</CEsurtax>
<EXduty>0.00</EXduty>
<IMvat>5000000.00</IMvat>
<SYSfee>8000000.00</SYSfee>
<AItax>2000000.00</AItax>
<Cduty>1000000.00</Cduty>
</Table> <Table diffgr:id="Table1"
msdata:rowOrder="1">
<Assessment>CHR/A167/2009</Assessment>
<Issue_Date>20/10/2009</Issue_Date>
<Due_Date>01/11/2009</Due_Date>
<Interest>2.00</Interest>
<Summary>BENTLEY 2009</Summary>
<Amount_Due>24000000.00</Amount_Due>
<IEduty>3000000.00</IEduty>
<LEsurtax>4000000.00</LEsurtax>
<CEsurtax>5000000.00</CEsurtax>
<EXduty>0.00</EXduty>
<IMvat>1000000.00</IMvat>
<SYSfee>8000000.00</SYSfee>
<AItax>2000000.00</AItax>
<Cduty>1000000.00</Cduty>
</Table>
<Table diffgr:id="Table1" msdata:rowOrder="2">
<Assessment>CHR/A196/2009</Assessment>
<Issue_Date>11/11/2009</Issue_Date>
<Due_Date>21/11/2009</Due_Date>
<Interest>2.00</Interest>
<Summary>BENTLEY 2009</Summary>
<Amount_Due>20000000.00</Amount_Due>
<IEduty>3000000.00</IEduty>
<LEsurtax>4000000.00</LEsurtax>
<CEsurtax>5000000.00</CEsurtax>
<EXduty>0.00</EXduty>
<IMvat>1000000.00</IMvat>
<SYSfee>4000000.00</SYSfee>
<AItax>2000000.00</AItax>
<Cduty>1000000.00</Cduty>
</Table> </NewDataSet> </diffgr:diffgram> </DataSet>
© Stack Overflow or respective owner