none
Problems reading XML into ASP.NET WebForm

    Question

  • I'm a hobbyist. I've built a few Windows VB projects but have never built a Web app. I'm reading through an admittedly rather dated ASP.NET book. There's one short tutorial that I just cannot get to work and I cannot find the error in the code...

    The WebForm should load a simple XML file into a dataset, bind it to a DataGrid server control and display it in the browser window. Instead of a DataGrid, the XML appears as a single string. It's as if the XML schema is not being recognized and the file is simply loading as a single string.

    The Vb is as follows:

    <%@ Page language="VB" runat="server" %>
    <%@ Import namespace="System.Data" %>
    <%@ Import namespace="System.XML" %>
    
         
    <script language="vb" runat="server">
    	Sub Page_Load()
    		Dim xmlfileName as string
    		xmlfileName="C:\BegASPNET\Ch02\addressbook.xml"
    		Dim addressDataSet as New DataSet
    		addressDataSet.ReadXML(xmlfileName)
    		Grid1.DataSource = addressDataSet
    		Grid1.DataBind()
    	End Sub
    </script>
    
    <html>
    <header>
    	<title>Address Book</title>
    </header>
    	<body
    		<asp:DataGrid id="Grid1" runat="server" /> <br />
    	</body>
    </html>

    The file is stored locally on my drive and it's recognized properly. The concatenated string that the browser displays is the correct file, trimmed of XML tags and spit out as one long string.
    It's not a problem with .NET or IIS installs. Other code is working fine. I've successfully bound and displayed tables from  SQLServer databases resident on my machine. It's just reading and displaying this XML that's problematic.

    The XML file is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <person>
    	<item>
    		<name>John Horn</name>
    		<address>108 Lawrence Ave</address>
    		<telephone>123-555-4874</telephone>
    		<cell>816-218-5555</cell>
    	</item>
    	<item>
    		<name>Susan Horn</name>
    		<address>108 Lawrence Ave</address>
    		<telephone>123-555-4874</telephone>
    		<cell>816-218-5555</cell>
    	</item>
    	<item>
    		<name>Ellen Horn</name>
    		<address>34 Fillmore Ave</address>
    		<telephone>508-555-4655</telephone>
    		<cell>508-999-4333</cell>
    	</item>
    </person>

    The output that appears in the browser is just the concatenated text:

    nameaddresstelephonecell John Horn108 Lawrence Ave123-555-4874816-218-5555 Susan Horn108 Lawrence Ave123-555-4874816-218-5555 Ellen Horn34 Fillmore Ave508-555-4655508-999-4333

    rather than a DataGrid. I don't know whether the DataSet is not bound to the DataGrid or if ReadXML is not properly reading the XML file. I suspect the latter. Don't know what I'm doing wrong. This is basically a mockup of a tutorial right out of the book...

    Any help would be appreciated. Thanx.

    Sunday, April 02, 2017 5:33 AM

Answers

All replies

  • Try fixing the HTML mistakes:

    <html>

    <head>

        <title>Address Book</title>

    </head>

    <body>

        <asp:DataGrid ID="Grid1" runat="server" /> <br />

    </body>

    </html>

    Sunday, April 02, 2017 8:42 AM
  • Viorel,

    Thanks for the reply.

    Kind of a dumb mistake that I made with the HTML...

    Unfortunately, it's not the answer. The corrected html doesn't change the result. Even more surprising is the fact that the *incorrect* <header></header> is properly loaded by the browser. Go figure...

    Sunday, April 02, 2017 1:54 PM
  • Maybe show again your correct HTML.

    Sunday, April 02, 2017 2:49 PM
  • Viorel,

    Thanks again for taking the time...

    The corrected .aspx file is as follows:

    <%@ Page language="VB" runat="server" %>
    <%@ Import namespace="System.Data" %>
    <%@ Import namespace="System.XML" %>
    
         
    <script language="vb" runat="server">
    	Sub Page_Load()
    		Dim xmlfileName as string
    		xmlfileName="C:\BegASPNET\Ch02\addressbook.xml"
    		Dim addressDataSet as New DataSet
    		addressDataSet.ReadXML(xmlfileName)
    		Grid1.DataSource = addressDataSet
    		Grid1.DataBind()
    	End Sub
    </script>
    
    <html>
    <head>
    	<title>Address Book</title>
    </head>
    <body
    	<asp:DataGrid ID="Grid1" runat="server" /> <br />
    	The html is working!
    </body>
    </html>

    I added a line to the body to give some more feedback from the browser. ("The html is working.")

    The output from the browser has the same problem:

    nameaddresstelephonecell John Horn108 Lawrence Ave123-555-4874816-218-5555 Susan Horn108 Lawrence Ave123-555-4874816-218-5555 Ellen Horn34 Fillmore Ave508-555-4655508-999-4333
    The html is working!

    Sunday, April 02, 2017 4:26 PM
  • Try fixing the HTML mistake:

    <html>

    <head>

           <title>Address Book</title>

    </head>

    <body>

           <asp:DataGrid ID="Grid1" runat="server" /> <br />

           The html is working!

    </body>

    </html>

    Sunday, April 02, 2017 4:30 PM
  • I'm sorry. I just don't understand. The html that you posted is identical to the html in my file.

    Are you saying that it's wrong, or are you saying that what you posted is the correction?

    I've used the same code in a Windows Form, checked row counts and column counts. All correct.

    I've pulled data from it. All correct.

    even bound it to a DataGridView and it displays correctly. So there must be something wrong with the html as you suggest. I just don't know what it is. My limitation, I guess...

    Sunday, April 02, 2017 5:02 PM
  • Replace ‘<body’ with ‘<body>’.

    Sunday, April 02, 2017 5:26 PM
  • Holy cow! You have to be kidding me...

    How the heck did I miss that?!

    Everything works fine. I'm just not used to looking at XML or HTML so my eyes just glossed over it.

    Sorry for wasting your time and thank you for your help.

    Sunday, April 02, 2017 5:39 PM