469,270 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

when creating an xml document from a dataset

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

Nov 12 '05 #1
3 9340
Hi Tom,

As for the your question that how to add a "<?xml version="1.0" ?>"
declaraion before the dataset's output xml, I think you can try the
following means:

1. Directly using the REsponse.Write to write the "<?xml version="1.0" ?>"
out before write the dataset string. For example:
ds.EnforceConstraints = False
Response.ContentType = "text/xml"
Response.Write("<?xml version="1.0" ?>");
ds.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema)
2. Use a XmlDocument to load the DataSet's Xml first and add a "<?xml
version="1.0" ?>" declaraion via the "XmlDeclaration" class. Then output
the XmlDocument into the Response.OutputStream. This is a formal way, for
example:

private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
DataSet ds = GetDataSet();

doc.LoadXml(ds.GetXml());
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0","UTF-8",null);

//Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);

//clear the response's original content
Response.Clear();
Response.ClearContent();
Response.ContentType="text/xml";

doc.Save(Response.OutputStream);
Response.End();
}

In addition, here are some reference on using XMLDocument in MSDN:

#XmlDocument Class
http://msdn.microsoft.com/library/en...XmlXmlDocument
ClassTopic.asp?frame=true

#XmlDocument.CreateXmlDeclaration Method
http://msdn.microsoft.com/library/en...xmlxmldocument
classcreatexmldeclarationtopic.asp?frame=true

Hope also helps. 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
Nov 12 '05 #2
"Tom Vukovich" <tv*****@wpsenergy.com> wrote in message news:c4**********@newsfeed.norlight.net...
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
Nov 12 '05 #3
Hi Tom,

Have you had a chance to check out the suggstions in my last reply? Are
they helpful to you? If there're anything else we can help, 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

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Piyush Gupta | last post: by
4 posts views Thread by Nikhil Patel | last post: by
reply views Thread by c.w.browne | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.