C# VS 2005 : Reading XML file doesn't work

  • Thread starter Thread starter milkyway
  • Start date Start date
M

milkyway

Hi ;-)

I have created a .aspx file that has the following:

DataSet ds = new DataSet();

ds.ReadXml(Server.MapPath("TestInput.xml"));

As far as I can tell, the first line does not work - at all. I just get
"error" in the status field of the browser. How can I check to see what
the problem is for it not executing? I don't know how to check the
return code or use the debugger with this.

Is there something wrong with what I have written altogether?

TIA
 
I was following the examples I had seen on the internet to make the
code work.

I had wanted to get a list of the items on the server and manipulate
the list (on the server).

Is there something I did wrong with what I have used? Is there a pit
that I fell into (perhaps with the creation of the .xml) that would
cause problems? Should I place .xml files in a different directory?

I guess I am kinda lost here as to where I can look for the problem or
how one might use the debugger to track it down ;-(
 
re:
Is there something I did wrong with what I have used?

The two lines of code you sent are not enough to determine that.

Offhand, did you import the system.data namespace ?

<%@ Import Namespace="System.Data" %>

re:
Is there a pit that I fell into (perhaps with the creation of the .xml)
that would cause problems?

It's hard to tell unless you post the xml itself
or run the xml file through an xml validator.

If you want to do the latter, Peter's XML Editor will tell you
if the file's XML is well-formed when you attempt to load it.

Get it at : http://www.iol.ie/~pxe/index.html

re:
Should I place .xml files in a different directory?

You might as well, since with ASP.NET 2.0 xml data goes in the App_Data dir.

re:
I was following the examples I had seen on the internet to make the
code work.

The thing is that data access examples "on the internet", and particularly examples
of binding to xml datasources, are unnecessarily complicated and, if all you want
to do is display the contents of an xml file, just place your xml file in your
application's App_Data directory ( if one doesn't exist, create it ), change
the Datafile's name to your XML's filename, and run this extremely short sample :

ShowXML.aspx:
------------------
<%@ Page Language="VB"%>
<html>
<body>
<form id="showXML" runat="server">
<asp:XmlDataSource id="MySource" DataFile="~/App_Data/your.xml" runat="server"/>
<asp:TreeView
SkinId="Bookstore"
DataSourceId="MySource"
ExpandDepth="3"
MaxDataBindDepth="3"
runat="server"
/>
</form>
</body>
</html>
----------

Easier access to XML data sources is one of
the benefits which ASP.NET 2.0 brings us.




Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
Thanks response Juan - I found the problem ;-P

Basically, the C# code I wrote was to create a .html file for display.
But - what I forgot were the directives so that the C# could run in
peace. When I did:

<%

DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("TestInput.xml"));
%>

Then it worked ;-)
 
Back
Top