|
 |
aspx_beginners thread: xml reader
Message #1 by "Peter" <petemail@r...> on Sat, 9 Mar 2002 02:00:55 -0500
|
|
how do i read an xml file and display its content using .net?
-pete
Message #2 by "lena nyman" <lena_nyman@h...> on Mon, 11 Mar 2002 07:50:22 +0000
|
|
>From: "Peter" <petemail@r...>
>Reply-To: "aspx_beginners" <aspx_beginners@p...>
>To: "aspx_beginners" <aspx_beginners@p...>
>Subject: [aspx_beginners] xml reader
>Date: Sat, 9 Mar 2002 02:00:55 -0500
>
>how do i read an xml file and display its content using .net?
>-pete
>
>$subst('Email.Unsub').
Here is one example if you want all tag-names and attribute names
as well as their value.
<script runat="server">
sub Page_Load(obj as object, e as eventargs)
dim reader as XmlTextReader
dim i as integer
try
reader = new XmlTextReader(Server.MapPath("yourfile.xml"))
While reader.Read()
Select Case reader.NodeType
Case XMLNodeType.Element
if reader.HasAttributes then
for i = 0 to reader.AttributeCount -1
Response.Write("<b>" & reader.Name & "</b>" & " " &
reader.GetAttribute(i) & "")
next
Response.Write("<br>")
end if
Case XMLNodeType.Text
Response.Write("<b>" & reader.Name & "</b>" & " " & reader.Value
& "<br>")
End Select
End While
catch ex as Exception
Response.Write("couldn't find xml-file")
finally
reader.Close
end try
end sub
</script>
try this
regards Lena
_________________________________________________________________
Chatta med vänner online, prova MSN Messenger: http://messenger.msn.se
Message #3 by "Dan McKinnon" <mddonna@q...> on Tue, 12 Mar 2002 07:51:07
|
|
> how do i read an xml file and display its content using .net?
> -pete
Lena's answer is probably the right way, and I'm sure she knows a lot more
about this than I do. If all you want to do is display the content in the
xml file on a Web page, the quick and dirty (easy) way is demonstrated
in "Beginning ASP.NET using VB.NET" in chapter 12, a file you create
called ReadingXML.aspx that reads a file named employees.xml (which is
actually created from an Access database - easily - in the lesson before
using ASP.NET). I don't know what this method does with tag attributes,
and it doesn't show tag names really. The table that is converted from the
Access database to the xml file is easily displayed in a table on a Web
page. I don't know what it would do with an xml file that had data that
wasn't easily formatted into an html table.
Here is the code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim objDataSet As New DataSet()
objDataSet.Readxml(Server.MapPath("Employees.xml"))
dgEmployees.DataSource = objDataSet.Tables(0).DefaultView
dgEmployees.DataBind()
End Sub
</script>
<html>
<body>
<asp:datagrid id="dgemployees" runat="server" />
</body>
</html>
Pretty cool and simple enough for people like me. Just replace the xml
file named Employees.xml above with the name of your xml file, and make
sure it is in the same directory as the aspx file that calls it.
Dan
Message #4 by "Peter" <petemail@r...> on Tue, 12 Mar 2002 12:14:27 -0500
|
|
Dan, thanks for the code this is what I was looking for. Last night I found
a similar C# script in a Web article.
http://stardeveloper.com/go/002E
The two major differences I found are:
1. It's not necessary to import OleDb
<%@ Import Namespace="System.Data.OleDb" %>
2. The data source line can be simplified
dgID.DataSource = ds;
------------------
dgID.DataSource = ds;
<%@ Import Namespace="System.Data" %>
<script Language="c#" runat="server">
void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Employees.xml"));
dgID.DataSource = ds;
dgID.DataBind();
}
</script>
<html><body>
<asp:DataGrid id="dgID" runat="server" />
</body></html>
-----------------------------
Every time I searched the Web, I kept finding lots of complicated code
written in different ways to read XML files, similar to Lena's example. I
assume other methods must give more functionality and performance?
-Peter
-----Original Message-----
> how do i read an xml file and display its content using .net?
> -pete
...If all you want to do is display the content in the
xml file on a Web page, the quick and dirty (easy) way is demonstrated
in "Beginning ASP.NET using VB.NET" ...
Here is the code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim objDataSet As New DataSet()
objDataSet.Readxml(Server.MapPath("Employees.xml"))
dgEmployees.DataSource = objDataSet.Tables(0).DefaultView
dgEmployees.DataBind()
End Sub
</script>
<html>
<body>
<asp:datagrid id="dgemployees" runat="server" />
</body>
</html>
Pretty cool and simple enough for people like me. Just replace the xml
file named Employees.xml above with the name of your xml file, and make
sure it is in the same directory as the aspx file that calls it.
Message #5 by "dwarakanath h" <madivi2002@y...> on Thu, 28 Mar 2002 21:12:59
|
|
Lena and Dan told are both correct. The only difference being, in Lenas
code the process loops thru the whole of XML file and writes it to the
browser using the traditional ASP technique (Response object) ..old habits
die hard :o) and Dan binds it to datagrid which is considered to be the
latest choice of doing it. If the XML files is huge I would recommend to
use the databinding as Dan did. Depending on ur requirement u could opt
what ever is appropriate.
Thanks
Dwarka
|
|
 |