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

XMLWriter Anyway to produce Doc with no encoding Info>

Bob
Hi
Need to produce a Doc with no encoding info.
Is there anyway of doing this?
Thanks
Bob
i.e.
<?xml version=\"1.0\" ?>


Mar 16 '06 #1
4 5533
Bob <bo*@nowhere.com> wrote:
Need to produce a Doc with no encoding info.
Is there anyway of doing this?
Thanks
Bob
i.e.
<?xml version=\"1.0\" ?>


If you use XmlTextWriter and *explicitly* specify a null encoding, I
believe that will work - but I haven't tried it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 17 '06 #2
Bob
Hi Jon,
Thanks for your reply.
I have done a bit more investigation since posting and the problem is that I
am writing into a stringbuilder.
It was reported previously as a bug by someone else but the MS response was
that it was by design. If you are writing to a stringbuilder then you will
end up with a string and strings are always UTF-16 Q.E.D.
I find this kind of logic dangerous.
It removes a level of control that I believe should remain with the
programmer.
For what ever reason, the URI that I am posting to insists on no coding
attribute.

So my logic of MakeXMLDoc -> string -> bytearray -> webrequest poststream is
now
MakeXMLDOc ->string->modified string -> bytearray WebRequest.

I am using Framework 2 and the recommendation is to use the XMLWriter.

I tried using a memorystream and a XMLWriterSettings class with encoding =
null but this didn't work.

I dare say there is probably a better way to solve my problem but I still
feel that making assumptions about the 'use' of classes at the framework
level is short sighted and bad design.

I would rather have my strings error and work my way back to some help that
said use a XMLWriterSetting class with encoding set to UTF-16 ( If I was
really going to use the string )
regards
Bob

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bob <bo*@nowhere.com> wrote:
Need to produce a Doc with no encoding info.
Is there anyway of doing this?
Thanks
Bob
i.e.
<?xml version=\"1.0\" ?>


If you use XmlTextWriter and *explicitly* specify a null encoding, I
believe that will work - but I haven't tried it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 17 '06 #3
Bob wrote:
Thanks for your reply.
I have done a bit more investigation since posting and the problem is that I
am writing into a stringbuilder.
It was reported previously as a bug by someone else but the MS response was
that it was by design. If you are writing to a stringbuilder then you will
end up with a string and strings are always UTF-16 Q.E.D.
I find this kind of logic dangerous.
Yes - it's frankly ridiculous. They can't tell what encoding you'll end
up using for converting the text data into a binary representation.
It removes a level of control that I believe should remain with the
programmer.
Agreed.
For what ever reason, the URI that I am posting to insists on no coding
attribute.

So my logic of MakeXMLDoc -> string -> bytearray -> webrequest poststream is
now
MakeXMLDOc ->string->modified string -> bytearray WebRequest.

I am using Framework 2 and the recommendation is to use the XMLWriter.

I tried using a memorystream and a XMLWriterSettings class with encoding =
null but this didn't work.


I think what you want is this:

public class NullEncodingStringWriter : StringWriter
{
public override Encoding Encoding
{
get { return null; }
}
}

If you create one of those, pass that to the XmlTextWriter, then call
XmlDocument.Save, you'll find that it doesn't put on the encoding.
Here's a sample:

using System;
using System.IO;
using System.Text;
using System.Xml;

public class Test
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();

doc.LoadXml ("<?xml version='1.0'
encoding='UTF-8'?><element>text</element>");

StringWriter sw = new EncodingStringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
doc.Save(xtw);
Console.WriteLine(sw.ToString());
}
}

public class NullEncodingStringWriter : StringWriter
{
public override Encoding Encoding
{
get { return null; }
}
}

Alternatively, if you want to go to a MemoryStream anyway (to get the
bytes out directly) you could use a StreamWriter which takes the
MemoryStream as the stream to write to and uses null as the encoding.
Unfortunately, StreamWriter prevents you from specifying a null
encoding, so you need to create a derived type which overrides the
Encoding property. Again, here's a sample:

using System;
using System.IO;
using System.Text;
using System.Xml;

public class Test
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();

doc.LoadXml ("<?xml version='1.0'
encoding='UTF-8'?><element>text</element>");

MemoryStream stream = new MemoryStream();
StreamWriter writer = new NullEncodingStreamWriter (stream);
XmlTextWriter xtw = new XmlTextWriter(writer);
doc.Save(xtw);
Console.WriteLine(Encoding.UTF8.GetString(stream.T oArray()));
}
}

public class NullEncodingStreamWriter : StreamWriter
{
public override Encoding Encoding
{
get
{
return null;
}
}

public NullEncodingStreamWriter (Stream stream) : base (stream)
{
}
}

Jon

Mar 17 '06 #4
Bob
Hi Jon,
Thank you for the examples.
I'll implement them and have a play around.
much appreciated.
Regards
Bob
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Bob wrote:
Thanks for your reply.
I have done a bit more investigation since posting and the problem is that I am writing into a stringbuilder.
It was reported previously as a bug by someone else but the MS response was that it was by design. If you are writing to a stringbuilder then you will end up with a string and strings are always UTF-16 Q.E.D.
I find this kind of logic dangerous.


Yes - it's frankly ridiculous. They can't tell what encoding you'll end
up using for converting the text data into a binary representation.
It removes a level of control that I believe should remain with the
programmer.


Agreed.
For what ever reason, the URI that I am posting to insists on no coding
attribute.

So my logic of MakeXMLDoc -> string -> bytearray -> webrequest poststream is now
MakeXMLDOc ->string->modified string -> bytearray WebRequest.

I am using Framework 2 and the recommendation is to use the XMLWriter.

I tried using a memorystream and a XMLWriterSettings class with encoding = null but this didn't work.


I think what you want is this:

public class NullEncodingStringWriter : StringWriter
{
public override Encoding Encoding
{
get { return null; }
}
}

If you create one of those, pass that to the XmlTextWriter, then call
XmlDocument.Save, you'll find that it doesn't put on the encoding.
Here's a sample:

using System;
using System.IO;
using System.Text;
using System.Xml;

public class Test
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();

doc.LoadXml ("<?xml version='1.0'
encoding='UTF-8'?><element>text</element>");

StringWriter sw = new EncodingStringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
doc.Save(xtw);
Console.WriteLine(sw.ToString());
}
}

public class NullEncodingStringWriter : StringWriter
{
public override Encoding Encoding
{
get { return null; }
}
}

Alternatively, if you want to go to a MemoryStream anyway (to get the
bytes out directly) you could use a StreamWriter which takes the
MemoryStream as the stream to write to and uses null as the encoding.
Unfortunately, StreamWriter prevents you from specifying a null
encoding, so you need to create a derived type which overrides the
Encoding property. Again, here's a sample:

using System;
using System.IO;
using System.Text;
using System.Xml;

public class Test
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();

doc.LoadXml ("<?xml version='1.0'
encoding='UTF-8'?><element>text</element>");

MemoryStream stream = new MemoryStream();
StreamWriter writer = new NullEncodingStreamWriter (stream);
XmlTextWriter xtw = new XmlTextWriter(writer);
doc.Save(xtw);
Console.WriteLine(Encoding.UTF8.GetString(stream.T oArray()));
}
}

public class NullEncodingStreamWriter : StreamWriter
{
public override Encoding Encoding
{
get
{
return null;
}
}

public NullEncodingStreamWriter (Stream stream) : base (stream)
{
}
}

Jon

Mar 17 '06 #5

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

Similar topics

6
by: Michael Rozdoba | last post by:
Am I going nutty or did the Firefox WebDeveloper toolbar display all images referenced within a page, via Information -> View Page Information -> Media? With 0.8 &...
7
by: Steven Reddie | last post by:
My problem starts with wanting " " to actually appear that way in the output rather than an actual encoded 0xA0 byte in the output stream. I thought a way to solve this would be to select...
4
by: Shailendra Batham | last post by:
Hi guys,Does any1 know what this error is all about, what I am trying to do is deserialize a XML, below is my code, let me know what I am doing wrongpublic class test{xin = "<?xml version='1.0'...
2
by: Fredrik Melin | last post by:
Hi, I have a vendor that requires me to send empty value, e.g. <OrderIDInfo /> problem is that my xslt need to add attributes, doing this <OrderIDInfo> <xsl:attribute name="orderID">...
2
by: Chuck | last post by:
Is there any way to export the report that is generated when using the "Documenter" of Access XP to another file such as Word, PDF, etc. Thanks for any help, Chuck
12
by: Christian Roth | last post by:
Hello, I am merely asking this for my own understanding: Processing instruction's data part is not entity-aware, i.e. character and numercial entities are not resolved at parsing time. E.g., ...
4
by: Christofer Dutz | last post by:
Hi, I am having a small problem, that is driving me nuts. My application reads some Xml and runs 2 Xsl Transformations to generate HTML. As soon as my second XSL introduces some <br/tags, the...
6
by: =?Utf-8?B?QmlsbEF0V29yaw==?= | last post by:
Hi, I'm defining a report layout using an XML document, plugging data values into fields before outputting the whole doc as an HTML page. I wrap each of the data fields in CDATA section (in case...
0
by: =?Utf-8?B?Qy4gSHVnaGVz?= | last post by:
Hello, I have a .net 2.0 application using a 'Settings.settings' configuration file to store application settings. The settings are modified at runtime and stored when the user exits the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.