473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do you output XML from ASP.NET?

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
10 54918
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
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
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
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
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
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
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
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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
0
by: newbie | last post by:
i'm a newbie of c language. can anyone help me to implement the code so that I can get the ciphertext from the output. thanks. #ifndef _3DES_H #define _3DES_H #ifndef uint8 #define uint8 ...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.