Link to home
Create AccountLog in
Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on

Exception Details: System.Xml.XmlException: Data at the root level is invalid. Line 2, position 1.

Hello expert,
Was able to convert stand alone aspx file to aspx with code behind. Now trying to morph to write
from a different XML file. Trying to get the program to just write the question numbers out.
Getting error message
Exception Details: System.Xml.XmlException: Data at the root level is invalid. Line 2, position 1.
Visual Studio compiles ok but the browser says tehre is a server error
Line 43:                   while(objXmlRdr.Read())

The aspx page is simple it just uses a label comtrol with the ID 'output; to write the string
from reading the XML page. It is writtem after the aspx.cs page.

Thanks,

Allen


*********************************reader3.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace UML_08
{
      /// <summary>
      /// Summary description for Reader3.
      /// </summary>
      public class reader3 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Label output;
            
            void Page_Load(object sender, EventArgs e)
            {
                  //http://www.aspfree.com/c/a/XML/XML-Processing-with-the-XMLReader-Object-1/4/

            
                  // variable to store the
                  // Xml file (with location
                  string strXmlRdr = "http://localhost/UML_08/UML.xml";
                  output.Text="<B>UML Fundamental Test</B>";
 
                  // create an instance of the
                  // XmlDocument object
                  XmlTextReader objXmlRdr =
                        new XmlTextReader(strXmlRdr);
 
                  objXmlRdr.WhitespaceHandling =
                        WhitespaceHandling.None;
                  output.Text += "<ul>";
 
                  while(objXmlRdr.Read())
                  {
                         if(objXmlRdr.NodeType ==
                              XmlNodeType.Element)
                        {
 
                              if(objXmlRdr.Name == "QUESTION")
                              {
                                    output.Text += objXmlRdr.ReadString();
                                          
                              }
       
                        }
                        else if(objXmlRdr.NodeType == XmlNodeType.EndElement);
                        
                        }
 
                              output.Text += "</ul>";
 
                        // close the object and free
                        // up memory
                        objXmlRdr.Close();
                  }
            

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}

+++++++++++++++++++++++++++++++reader3.aspx.
<%@ Page language="c#" Codebehind="reader3.aspx.cs" AutoEventWireup="false" Inherits="UML_08.reader3" %>
<HTML>
      <HEAD>
            <link rel="stylesheet" type="text/css" href="UML_StyleSheet.css">
      </HEAD>
      <body>
            <asp:label id="output" runat="server" />
      </body>
</HTML>

+++++++++++++++++++++++++UML.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <TEST>

  <QUESTION>1</QUESTION>
        <A>A. edges by edges</A>
       <B>B. nodes by edges</B>
       <C>C. transitions by transitions</C>
       <D>D. nodes by nodes</D>
       <E>E. edges by nodes</E>
       <F>F. states by states</F>
 
 
 <QUESTION>2</QUESTION>
        <A>A. communication diagram</A>
        <B>B. class diagram</B>
        <C>C. interaction overview diagram</C>
        <D>D. sequence diagram</D>
        <E>E. composite structure diagram</E>
        <F>F. state machine diagram</F>

<QUESTION>3</QUESTION>
        <A>A. An interface may implement multiple classifiers.</A>
        <B>B. The set of interfaces implemented by a classifier are its provided interfaces.</B>
        <C>C. A classifier implementing an interface conforms to its contract.</C>
        <D>D. The set of interfaces implemented by a classifier are its required interfaces.</D>
        <E>E. A classifier may only implement one interface.</E>
.........................

 
ASKER CERTIFIED SOLUTION
Avatar of Gary Davis
Gary Davis
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of nsanga
nsanga

open the xml UML.xml directly in any browser ( IE) and whether it is opened
Avatar of Allen Pitts

ASKER

Thanks. That was it.
Thanks. One point in looking at your XML, you should encapsulate the answers within each question. In this case I am using an attribute to hold the question number:
<?xml version="1.0" encoding="ISO-8859-1" ?> 
<TEST>
  <QUESTION NUMBER="1">
       <A>A. edges by edges</A> 
       <B>B. nodes by edges</B> 
       <C>C. transitions by transitions</C> 
  </QUESTION>
  <QUESTION NUMBER="2">
        <A>A. communication diagram</A> 
        <B>B. class diagram</B> 
        <C>C. interaction overview diagram</C> 
        <D>D. sequence diagram</D> 
  </QUESTION>
  <QUESTION NUMBER="3">
        <A>A. An interface may implement multiple classifiers.</A> 
        <B>B. The set of interfaces implemented by a classifier are its provided interfaces.</B> 
        <C>C. A classifier implementing an interface conforms to its contract.</C> 
  </QUESTION>

Open in new window