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

Suppress encoding attribute - XMLTextWriter

I'm using the XMLTextWriter to create an XML document. I do not want the
encoding attribure in the XML file.

Instead of:
<?xml version="1.0" encoding="utf-8"?>

I want:
<?xml version="1.0"?>

Code extract:
MemoryStream ms= new MemoryStream();
XmlTextWriter x = new XmlTextWriter(ms, Encoding.UTF8);
Nov 12 '05 #1
2 7186


Josh Newman wrote:
I'm using the XMLTextWriter to create an XML document. I do not want the
encoding attribure in the XML file.

Instead of:
<?xml version="1.0" encoding="utf-8"?>

I want:
<?xml version="1.0"?>
Why? If you save as UTF-8 there is nothing wrong with encoding="utf-8"
in the XML declaration.
Code extract:
MemoryStream ms= new MemoryStream();
XmlTextWriter x = new XmlTextWriter(ms, Encoding.UTF8);


The XML declaration is written by the WriteStartDocument method but this
doesn't seem to have an overload to suppress the encoding.
I guess it should be possible to write your own class extending
XmlTextWriter where you then override the method WriteStartDocument to
make sure it only outputs the XML declaration with the version but with
no encoding. I have not tested that approach though.

As a sort of hack you could also try to use XmlTextWriter and then not
to call WriteStartDocument but rather WriteProcessingInstruction with
the proper arguments so that a processing instruction looking like an
XML declaration is written. But that is certainly a hack and I haven't
tested that either.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
I need to remove the "encoding" part because the XML parser on one of our
legacy systems blows-up if you include and encoding.
It is a third-party component that we cannot change so I have no choice but
to remove it.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:OI**************@TK2MSFTNGP14.phx.gbl...


Josh Newman wrote:
I'm using the XMLTextWriter to create an XML document. I do not want the
encoding attribure in the XML file.

Instead of:
<?xml version="1.0" encoding="utf-8"?>

I want:
<?xml version="1.0"?>


Why? If you save as UTF-8 there is nothing wrong with encoding="utf-8" in
the XML declaration.
Code extract:
MemoryStream ms= new MemoryStream();
XmlTextWriter x = new XmlTextWriter(ms, Encoding.UTF8);


The XML declaration is written by the WriteStartDocument method but this
doesn't seem to have an overload to suppress the encoding.
I guess it should be possible to write your own class extending
XmlTextWriter where you then override the method WriteStartDocument to
make sure it only outputs the XML declaration with the version but with no
encoding. I have not tested that approach though.

As a sort of hack you could also try to use XmlTextWriter and then not to
call WriteStartDocument but rather WriteProcessingInstruction with the
proper arguments so that a processing instruction looking like an XML
declaration is written. But that is certainly a hack and I haven't tested
that either.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Lacey | last post by:
Is anyone aware why the following code (intended to write XML into a memory-based XmlTextWriter, and return the complete document as a string) produces badly formed XML due to the resultant string...
1
by: dheeraj dhondalkar via .NET 247 | last post by:
(Type your message here) Hi, static string SerializeThingToXmlString(object thing) { StringWriter stringWriter = new StringWriter(); XmlSerializer serializer = new...
4
by: z. f. | last post by:
i have xml with the line <VAL ID="artist" VAL="abc & cde"/> i need the & character to be there but the xmlDocument Load method throw exception for invalid character. i don't want to encode...
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
5
by: Waldy | last post by:
Hi there, how do you set the encoding format of an XML string? When I was outputting the XML to a file you can specify the encoding format like so: XmlTextWriter myWriter; myWriter = new...
4
by: Bob | last post by:
Hi Need to produce a Doc with no encoding info. Is there anyway of doing this? Thanks Bob i.e. <?xml version=\"1.0\" ?>
4
by: Terry Olsen | last post by:
I use the following code to create an XML string: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tw As New StringWriter Dim xml...
7
by: John Hopper | last post by:
Hello, Using code below to create an xml file, how do I specifiy utf-8 encoding in the root element, ie create "<?xml version="1.0" encoding="utf-8"?> Thanks! ...
23
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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...

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.