473,287 Members | 1,868 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,287 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 5529
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.