when creating an xml document from a dataset

  • Thread starter Thread starter Tom Vukovich
  • Start date Start date
T

Tom Vukovich

and returning the xml to the requesting web page, how do you insert the XML
declaration?

ds.EnforceConstraints = False
Response.ContentType = "text/xml"
ds.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema)

I would like to add

<?xml version="1.0" ?>

to the top of the response stream.

Thanks for your help.

-tv
 
Hi Tom.

I've found that this post is a duplicated one with another in the
microsoft.public.dotnet.xml newsgroup. I've posted a reply to you in that
thread. Please have a look and if you feel it convenient that we continue
to discuss in that thread, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Tom Vukovich said:
ds.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema)

I would like to add

<?xml version="1.0" ?>

to the top of the response stream.

Well, for starters, have you tried,

new StreamWriter( Response.OutputStream).Write( "<?xml version=\"1.0\" ?>");

prior to calling ds.WriteXml( )?

If you want greater control over the XML output, then you should
wrap the DataSet in an XmlDataDocument. However, to manipulate
the XmlDataDocument's formatting you must turn off constraints in
the DataSet (presumably when rendering a response to an HTTP
request, the DataSet is about to go away just the same so this may
be acceptable).

XmlDataDocument xmlDoc = new XmlDataDocument( ds);
xmlDoc.DataSet.EnforceConstraints = false;
XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration( "1.0", null, null);
xmlDoc.PrependChild( xmlDecl);
xmlDoc.WriteTo( new XmlTextWriter( Response.OutputStream));

Advantages to this approach include the ability to do a number of
things to the XML output, including nicely indented and formatted
XML,

XmlDataDocument xmlDoc = new XmlDataDocument( ds);
xmlDoc.DataSet.EnforceConstraints = false;
XmlTextWriter xmlSink = new XmlTextWriter( Response.OutputStream);
xmlSink.Formatting = Formatting.Indented;
xmlSink.Indentation = 4;
xmlDoc.WriteTo( xmlSink);


Derek Harmon
 
Back
Top