473,396 Members | 1,840 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,396 software developers and data experts.

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

Similar topics

0
by: Anita C | last post by:
Hi, I have an xml document (Xml1.xml) with the foll.structure: <?xml version="1.0" encoding="utf-8"?> <REPORTS> <SCOPE TITLE="My Reports"> <REPORT URL="Report1.aspx" AUTHOR="me">First...
9
by: Paul | last post by:
Hi all Arggghhh........... The problem.....I want the user to be able to create an excel document and name particular cells in the document where they want the data to be placed and then save...
1
by: Piyush Gupta | last post by:
Hi All, I am using following code snippet to add another user in Users.xml: ------------------------------------------------------------------- DataSet dstUsers = new DataSet();...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
10
by: Steven Blair | last post by:
Hi, Quick overview of the problem: public bool Something( out DataSet ds ) { bool ret=false; try {
4
by: Nikhil Patel | last post by:
Hi all, I need to generate a word document and save it on the server from an ASP.Net application. Basically I want to load a word template and insert some field values from a dataset and save...
0
by: c.w.browne | last post by:
Hi, Ive had a bit of a look around for other people with this problem and cant find anything that solves it in my case, so I'm afraid im going to have to bother you all with a post of my own. ...
0
by: GovEgg | last post by:
Hi All, I am new to writing xml in .net and am wondering how to get my xml document to start with the following startelement?... - <EMSDataSet xmlns="http://www.nemsis.org"...
3
by: nuchphasu | last post by:
Hi I have a problem on Dropdownlist that connect database and retrieve data by Ajax.I write javascript like this -------------------------------------------------------------------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.