By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

How do you output XML from ASP.NET?

P: n/a
I am wondering how to make ASP.NET output XML instead of HTML.

Here is my situation:

I am using IE as a front-end to browse company documents. Let's say,
a user wants a Word document called foobar, then they'd do a http
request for "getdoc.aspx?foobar"

Now what getdoc.aspx does is it gets the Word document from the server
file system, does an XSLT transform on it to hyperlink it to other
documents and then returns the hyperlinked WordML document to the
browser.

What I want is for the IE to display the transformed Word document in
the browser's Word Add-In.

My problem is that ASP.NET seems to want to return HTML, not XML/
WordML.

Using an XmlWriter I am able to stream the WordML markup to the
browser, but I get a DOCTYPE and HTML code appended to the end, so the
Word Add-In won't touch it.

I tried deleting the DOCTYPE and HTML markup from the page design
source in Visual Studio, but then VS complains "Validation (): Element
'html' occurs too few times".

I know I can transform the WordML into HTML using WordML2HTML.xslt,
but this is both slow and not as pretty as loading a WordML into the
browser Add-In. (I want the client to deal with processing the WordML
tags.)

I know I could return XML via a WebService, but I don't want to deal
with the WSDL. All I want is for IIS to return the raw XML/WordML so
that the browser can load it.

Is there some special settings or DOCTYPE I need to set in Visual
Studio to get ASP to return XML or is a Web Form the wrong tool for
what I'm trying to do?

thanks
paul

May 17 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi,

It's pretty easy. You just need to set the right content type (text/xml) and
just pump out the XML. For a sample, look at the section "Emitting XML
Content to the Browser Window Directly" on
http://aspnet.4guysfromrolla.com/articles/092403-1.aspx

"li******@aol.com" wrote:
I am wondering how to make ASP.NET output XML instead of HTML.

Here is my situation:

I am using IE as a front-end to browse company documents. Let's say,
a user wants a Word document called foobar, then they'd do a http
request for "getdoc.aspx?foobar"

Now what getdoc.aspx does is it gets the Word document from the server
file system, does an XSLT transform on it to hyperlink it to other
documents and then returns the hyperlinked WordML document to the
browser.

What I want is for the IE to display the transformed Word document in
the browser's Word Add-In.

My problem is that ASP.NET seems to want to return HTML, not XML/
WordML.

Using an XmlWriter I am able to stream the WordML markup to the
browser, but I get a DOCTYPE and HTML code appended to the end, so the
Word Add-In won't touch it.

I tried deleting the DOCTYPE and HTML markup from the page design
source in Visual Studio, but then VS complains "Validation (): Element
'html' occurs too few times".

I know I can transform the WordML into HTML using WordML2HTML.xslt,
but this is both slow and not as pretty as loading a WordML into the
browser Add-In. (I want the client to deal with processing the WordML
tags.)

I know I could return XML via a WebService, but I don't want to deal
with the WSDL. All I want is for IIS to return the raw XML/WordML so
that the browser can load it.

Is there some special settings or DOCTYPE I need to set in Visual
Studio to get ASP to return XML or is a Web Form the wrong tool for
what I'm trying to do?

thanks
paul

May 17 '07 #2

P: n/a
On May 17, 3:49 am, linha...@aol.com wrote:
Using an XmlWriter I am able to stream the WordML markup to the
browser, but I get a DOCTYPE and HTML code appended to the end, so the
Word Add-In won't touch it.
Try to set Response.ContentType

Response.Clear();
Response.ContentType = "text/xml";
Response.Write(xml);

May 17 '07 #3

P: n/a
Just a suggestion...in your Page_Load event, try adding this at the
top:

Response.ContentType = "text/xml; charset=utf-8";

On May 16, 9:49 pm, linha...@aol.com wrote:
I am wondering how to make ASP.NET output XML instead of HTML.

Here is my situation:

I am using IE as a front-end to browse company documents. Let's say,
a user wants a Word document called foobar, then they'd do a http
request for "getdoc.aspx?foobar"

Now what getdoc.aspx does is it gets the Word document from the server
file system, does an XSLT transform on it to hyperlink it to other
documents and then returns the hyperlinked WordML document to the
browser.

What I want is for the IE to display the transformed Word document in
the browser's Word Add-In.

My problem is that ASP.NET seems to want to return HTML, not XML/
WordML.

Using an XmlWriter I am able to stream the WordML markup to the
browser, but I get a DOCTYPE and HTML code appended to the end, so the
Word Add-In won't touch it.

I tried deleting the DOCTYPE and HTML markup from the page design
source in Visual Studio, but then VS complains "Validation (): Element
'html' occurs too few times".

I know I can transform the WordML into HTML using WordML2HTML.xslt,
but this is both slow and not as pretty as loading a WordML into the
browser Add-In. (I want the client to deal with processing the WordML
tags.)

I know I could return XML via a WebService, but I don't want to deal
with the WSDL. All I want is for IIS to return the raw XML/WordML so
that the browser can load it.

Is there some special settings or DOCTYPE I need to set in Visual
Studio to get ASP to return XML or is a Web Form the wrong tool for
what I'm trying to do?

thanks
paul

May 17 '07 #4

P: n/a
Thanks everyone somuch for the suggestions & links.

It's still not working. My code (which I adapted from an XSLT web
example) uses an Xmlwriter.Close() to output the xml to the response
stream. I guess I don't know how to use it with Response.Write. I'm
reading through the 4guysfromrolla example which uses XmlTextWriter
(is this the same or different than Xmlwriter?).

I'm still getting the same problem which is garbage DOCTYPE & markup
appended to the end of my XML document which keeps the browser from
launching

The browser is getting XML back from the server, but I'm still getting
the DOCTYPE appended. There output/error I'm getting from the browser
is:

<?mso-application progid="Word.Document"?>
<wsp:wordDocument xmlns:wsp="http://schemas.microsoft.com/office/word/
2003/wordml/sp2"
.....{lengthy WordML content here}...
<w:r wsp:rsidR="00266357">
<w:tab wx:wTab="3300" wx:tlc="none

The XML page cannot be displayed Cannot view XML input using XSL
style sheet. Please correct the error and then click the Refresh
button, or try again later.

Cannot have a DOCTYPE declaration outside of a prolog. Error
processing resource 'http://localhost:3394/myPage.aspx?cn=Conf...

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

==================

My code is more or less:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim CNPath As String = "http://Localhost/myVirtualDirectory/
myWordML.xml"
Dim XSLT_Path As String = "http://Localhost/myVirtualDirectory/
myXSL.xslt"

' Load the style sheet.
Dim xslt As New XslCompiledTransform(True) 'Enable XSLT
debugging
xslt.Load(XSLT_Path)

' Create the writer.
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.IndentChars = vbTab
settings.ConformanceLevel = ConformanceLevel.Fragment

Response.ClearContent()
Response.ContentType = "text/xml; charset=utf-8"

Dim writer As XmlWriter =
XmlWriter.Create(Response.OutputStream, settings)

Dim reader As XmlReader = XmlReader.Create(CNPath)

reader.MoveToContent()

xslt.Transform(reader, writer)

writer.Close()
reader.Close()
End Sub

======================

The default page markup (which gives me the ""Validation (): Element
'html' occurs too few times" error mentioned above if I try to delete
it) suppied by VS2005 is:

<%@ Page Language="vb" AutoEventWireup="false"
CodeBehind="CN_XML.aspx.vb" Inherits="DigitalBuildBook.CN_XML" %>

<!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" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

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

May 17 '07 #5

P: n/a
Okay...I deleted the markup and found a post that explained how to
turn off the HTML validation error message:
Validation(): Element 'html' occurs too few times.
Turn off validations under : Tools -Options -Text Editor -HTML -Validation
I'm still not getting the browser to automatically launch the WordML
Add-In as I wanted, but I am getting well formed XML back from IIS so
I guess the answer to my original question was answered.

Thanks again everyone for your help.

-Paul

May 17 '07 #6

P: n/a
On May 17, 8:58 pm, linha...@aol.com wrote:
>
I'm still not getting the browser to automatically launch the WordML
Add-In as I wanted, but I am getting well formed XML back from IIS so
Paul, try to set the Response.ContentType in one of the following
values

"application/msword"
"application/msword-xml"
"application/vnd.ms-word.document.12"

and also check the following article

http://support.microsoft.com/kb/888579

Hope it helps

May 17 '07 #7

P: n/a
On May 17, 3:51 pm, hazyshadeofg...@hotmail.com wrote:
Just a suggestion...in your Page_Load event, try adding this at the
top:

Response.ContentType = "text/xml; charset=utf-8";
XML files may open in Internet Explorer instead of opening in the
expected Office program when the ContentType header contains the
charset parameter
http://support.microsoft.com/kb/888579

May 17 '07 #8

P: n/a
On May 17, 1:57 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On May 17, 3:51 pm, hazyshadeofg...@hotmail.com wrote:
Just a suggestion...in your Page_Load event, try adding this at the
top:
Response.ContentType = "text/xml; charset=utf-8";

XMLfiles may open in Internet Explorer instead of opening in the
expected Office program when the ContentType header contains the
charset parameterhttp://support.microsoft.com/kb/888579
Well, I'm getting close.

Response.ContentType = "application/msword;charset=UTF-8"
Response.ContentType = "application/msword"

Both cause IE to launch Winword.exe which pops up a "Convert File"
dialog box with a selection of Word formats (e.g. RTF, HTML, old
versions of word, wordperfect). If I select XML, a blank Word
application window pops up, but then the browser window takes focus
and the content is loaded in the browser window formatted like a word
document.

The charset setting doesn't seem to make a difference

The other two settings you suggested:
"application/msword-xml"
"application/vnd.ms-word.document.12"
just cause the XML to be sent directly to the browser and shown in
tree format.

The good news is that IE is definitely launching Word and is able to
display the content in Word format.

That bad news is:
(1) Word demands a convert to XML step on the client step.
(2) Word opens an unnecessary Word window before loading the content
into the browser window.
Both seem like solvable issues if I find the right settings.

Good tip on the charset issue. I saved the Output and then loaded it
in notepad and the header was:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>

I guess I'm overriding the charset setting somewhere else (maybe in my
XSLT transformation?).

thanks again, you've been a big help,
-paul

May 18 '07 #9

P: n/a

<li******@aol.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
On May 17, 1:57 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
Both cause IE to launch Winword.exe which pops up a "Convert File"
dialog box with a selection of Word formats (e.g. RTF, HTML, old
versions of word, wordperfect). If I select XML, a blank Word
application window pops up, but then the browser window takes focus
and the content is loaded in the browser window formatted like a word
document.
The following code is working great for me without any prompt from Word
(2007). It means, that your output has any extra header that Word doesn't
like. Try to call Response.Clear() or Response.ClearHeaders() at the
beginning.

<script runat=server language="C#">
void Page_Load() {

Response.Clear();
Response.ContentType = "application/msword";

Response.Write(@"<?xml version=""1.0"" encoding=""UTF-8"" ?>
<?mso-application progid=""Word.Document""?>
<w:wordDocument
xmlns:w=""http://schemas.microsoft.com/office/word/2003/wordml"">
<w:body>
<w:p>
<w:r>
<w:t>My test document</w:t>
</w:r>
</w:p>
</w:body>
</w:wordDocument>");
}
</script>
May 18 '07 #10

P: n/a
On May 17, 1:57 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
On May 17, 3:51 pm, hazyshadeofg...@hotmail.com wrote:
Just a suggestion...in your Page_Load event, try adding this at the
top:
Response.ContentType = "text/xml; charset=utf-8";

XML files may open in Internet Explorer instead of opening in the
expected Office program when the ContentType header contains the
charset parameterhttp://support.microsoft.com/kb/888579
Got it. I deleted this line from my page_load:
settings.ConformanceLevel = ConformanceLevel.Fragment

I guess because I guess Word figured my response was an XML fragment
so it wasn't sure whether or not to load it.

Thanks so much for all your help, code samples & kb links. I really
appreciate it.

-Paul

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

' Load the style sheet.
Dim xslt As New XslCompiledTransform(True) 'Enable XSLT
debugging
xslt.Load("myXSLT.xslt")

' Create the writer.
Dim settings As New XmlWriterSettings()
settings.Indent = True
'settings.IndentChars = vbTab

' Force Response Output into XML instead of HTML
Response.ClearContent()
Response.ContentType = "application/msword"

'**Remove the charset from the Content-Type header.
Response.Charset = ""

Dim writer As XmlWriter =
XmlWriter.Create(Response.OutputStream, settings)

Dim reader As XmlReader = XmlReader.Create("myWordML.xml")

reader.MoveToContent()

' Execute the transformation.
xslt.Transform(reader, writer)

writer.Close()
reader.Close()
End Sub

May 22 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.