473,320 Members | 1,858 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,320 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 54902
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.