473,405 Members | 2,344 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,405 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 9549
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.