How do you output XML from ASP.NET?

L

linhardt

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
 
G

Guest

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);
 
H

hazyshadeofgrey

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

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

linhardt

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>
 
L

linhardt

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
 
G

Guest

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
 
L

linhardt

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
 
G

Guest

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>
 
L

Lefty

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top