DOCTYPE cannot be declared outside Prolog

Source: Internet
Author: User

The first attempt to display XML content on the ASP. Net page. The following problems were encountered:

 

After research and testing, it is found that the cause is as follows:

To display XML content on the page, you must place the following content in the code file (cs) on the page:

Response. charset = "gb2312"; Response. contentType = "text/xml"; Response. contentEncoding = System. text. encoding. getEncoding ("gb2312"); Pay special attention to the fact that only

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "test. aspx. cs" Inherits = "test" %>

Therefore, after creating an asp.net page, you must add the following parts:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

Delete. Otherwise, the problem mentioned above will occur. Because the specified Response. ContentType = "text/xml" in the CS file is associated with the tag <! DOCTYPE> conflict.

If the tag <! Delete the content in DOCTYPE> and run the page again. The error "XML document can only have one top-level element." appears .". Why? Careful analysis is not difficult to see. The XML content output through the page will have a top-level element <? Xml version = "1.0" encoding = "gb2312"?> . Because the page type is specified as xml, the xml document can have only one top-level element. Therefore, we need to delete all the labels in the design file (. aspx) of the page, leaving only

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "test. aspx. cs" Inherits = "test" %>

This line. It is not hard to see that this line is a server label and will not be sent to the client.

Therefore, if you want to display the XML document content on the page, there are two main points:

1. layout file on the page (. only <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "test. aspx. cs "Inherits =" test "%> one line;

2. Specify the encoding method for the content output to the client in the code file (. cs) on the page:

Response. Charset = "gb2312"; Response. ContentType = "text/xml"; Response. ContentEncoding = System. Text. Encoding. GetEncoding ("gb2312 ");

Then use Response. Write (XML); to output the XML content to the client. Of course, the content of XML itself must be correct. You can save the XML content as * by pasting it into notepad *. xml file. If you can use IE to open the correct display, the XML itself is correct. Otherwise, the XML itself is faulty.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/greatfeather/archive/2008/06/25/2585701.aspx

 

The XML page cannot be displayed.
XML input cannot be viewed using the XSL style sheet. Correct the error and click Refresh or try again later.

--------------------------------------------------------------------------------

DOCTYPE cannot be declared outside Prolog. An error occurred while processing the resource 'HTTP: // localhost: 3835/Default. aspx. Row 3rd, Location: 11

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "&...

Using System. Xml;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. ContentType = "text/xml ";
// Create an xmldocument object
XmlDocument xdoc = new XmlDocument ();
// Xml document path. The current path is the project path.
String strFileName = Page. MapPath (Page. AppRelativeTemplateSourceDirectory)
+ "TestDocs/sample. xml"; // relative path
XmlTextReader xreader = new XmlTextReader (strFileName );
Xreader. WhitespaceHandling = WhitespaceHandling. None;
While (xreader. Read ())
{
Switch (xreader. NodeType)
{
Case XmlNodeType. Element:
Page. Response. Write ("<" + xreader. Name + "> ");
Break;
Case XmlNodeType. Text:
Page. Response. Write (xreader. Value + "");
Break;
Case XmlNodeType. EndElement:
Page. Response. Write ("</" + xreader. Name + "> ");
Break;
Case XmlNodeType. Comment:
Page. Response. Write ("<! -- "+ Xreader. Value +" --> ");
Break;
Case XmlNodeType. XmlDeclaration:
Page. Response. Write ("<? Xml version = '1. 0'?> ");
Break;
Case XmlNodeType. Document:
Break;
Case XmlNodeType. DocumentType:
Page. Response. Write ("<! DOCTYPE "+ xreader. Name +" ["+ xreader. Value +"] ");
Break;
}
}
// Close xmlTextReader
If (xreader! = Null)
Xreader. Close ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.