Why doesn't it show the rest of the XML file?

A

Alan Silver

Hello,

I am just trying out reading an XML file in an ASP.NET page, but am
having a little trouble. I'm sure this is something really obvious and
dumb, but I can't see what ...

I have an XML file that contains some site map info (see further down),
and I have written a page to read this in and display the contents. I
put it into a datagrid and also use a repeater (the former was a quick
way of seeing what happened, the latter was in an attempt to have more
flexibility over what I do with the data).

The grid shows the five items in the XML file, but the repeater only
shows three. If you look at the source generated, it seems that the
other two entries are blank.

Anyone any idea why? Any help appreciated.

First the ASP.NET file ...

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<script runat="server">

void Page_Load(Object sender, EventArgs e) {
DataSet dstXML;

dstXML = new DataSet();
dstXML.ReadXml(MapPath("SiteMap.xml"));
// display the info in a repeater
rptSiteMap.DataSource = dstXML;
rptSiteMap.DataBind();
// display the info in a grid
dgrdData.DataSource = dstXML;
dgrdData.DataBind();
}

void rptSiteMap_Item(Object sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item) {
Literal litLink = (Literal)e.Item.FindControl("litLink");
litLink.Text = (string)DataBinder.Eval(e.Item.DataItem, "Title") + "
<small>(" + (string)DataBinder.Eval(e.Item.DataItem, "URL") +
")</small>";
}
}

</script>

<html>
<head>
<style type="text/css">
body, p, td, h1, h2, h3 {
font-family: Arial;
color: #0000ff;
background-color: #ffffff;
}
</style>
</head>

<body>
<form runat="server">
<p><asp:Repeater id="rptSiteMap" OnItemDataBound="rptSiteMap_Item"
Runat="Server">
<ItemTemplate><br><asp:Literal id="litLink" Runat="Server"
/></ItemTemplate>
</asp:Repeater></p>

<hr><br>

<asp:DataGrid id="dgrdData" Runat="Server" />

</form>
</body>
</html>


Now the XML file ...

<SiteMap>
<Page>
<URL>/</URL>
<Title>Daisy Documentation Centre</Title>
<Description>A collection of useful and interesting links, pages, files
and stuff.</Description>
</Page>

<Page>
<URL>/DotNET/</URL>
<Title>My DotNet Tests</Title>
<Description>My tests of ASP.NET and other things.</Description>
</Page>

<Page>
<URL>/ReadXML.aspx</URL>
<Title>Read An XML File</Title>
<Description>My working out how to read an XML file from disk, how to
cache it and how to display the contents.</Description>
</Page>

<Page>
<URL>/links.asp</URL>
<Title>Useful Links</Title>
<Description>Links to places of interest outside of the
DDC.</Description>
</Page>

<Page>
<URL>/servervars.asp</URL>
<Title>IIS6 ASP Server Variables</Title>
<Description>A listing of the server variables held by IIS when running
ASP (Classic, not .NET).</Description>
</Page>

</SiteMap>



and finally the HTML sent to the browser (for clarity, I've only posted
the relevant bits and ignored the view state info, etc) ...

<p><br>Daisy Documentation Centre <small>(/)</small><br><br>Read An XML
File <small>(/ReadXML.aspx)</small><br><br>IIS6 ASP Server Variables
<small>(/servervars.asp)</small></p>

<hr><br>

<table cellspacing="0" rules="all" border="1" id="dgrdData"
style="border-collapse:collapse;">
<tr>
<td>URL</td><td>Title</td><td>Description</td>
</tr><tr>
<td>/</td><td>Daisy Documentation Centre</td><td>A
collection of useful and interesting links, pages, files and stuff.</td>
</tr><tr>
<td>/DotNET/</td><td>My DotNet Tests</td><td>My tests of
ASP.NET and other things.</td>
</tr><tr>
<td>/ReadXML.aspx</td><td>Read An XML File</td><td>My
working out how to read an XML file from disk, how to cache it and how
to display the contents.</td>
</tr><tr>
<td>/links.asp</td><td>Useful Links</td><td>Links to
places of interest outside of the DDC.</td>
</tr><tr>
<td>/servervars.asp</td><td>IIS6 ASP Server
Variables</td><td>A listing of the server variables held by IIS when
running ASP (Classic, not .NET).</td>
</tr>
</table>
 
A

Alan Silver

Try changing this :
if (e.Item.ItemType == ListItemType.Item)

to this :

if((e.Item.ItemType == ListItemType.Item) ||
(e.Item.ItemType == ListItemType.AlternatingItem))

See the test at : http://asp.net.do/test/readXML.aspx

Doh, obvious eh?

Actually I did know about the AlternatingItem bit, but assuming
(incorrectly as it turned out) that since I had only used the
ItemTemplate in the repeater, that every item called would use that and
not the non-existent AlternatingItemTemplate.

You live and learn eh? Thanks very much for the reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top