Re: XMLWriter Anyway to produce Doc with no encoding Info>

Tech-Archive recommends: Fix windows errors by optimizing your registry



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.ToArray()));
}
}

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

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

Jon

.



Relevant Pages

  • Re: XMLWriter Anyway to produce Doc with no encoding Info>
    ... I tried using a memorystream and a XMLWriterSettings class with encoding ... public class NullEncodingStringWriter: StringWriter ... MemoryStream as the stream to write to and uses null as the encoding. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sql server to xml string with encoding
    ... But the data is stored in ISO-8859-9 in the database. ... encoding when writing to the stream instead of UTF-8 wich the data is not? ... > Rather than writing to a MemoryStream, write to a StringWriter. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Send string to IP address
    ... "Plain hex" implies something formatted as text, but doesn't answer the question of encoding. ... There's no "just" as far as "an ASCII string" is concerned. ... Characters are not bytes and bytes are not characters. ... Normally you'd create the Writer once at the same time as you create the underlying stream, rather than every time you write some text, obviously. ...
    (comp.lang.java.programmer)
  • Re: combine serveral .txt files
    ... Open the first stream to read from ... When you've finished all the files, close the output stream. ... the files are using the same encoding. ... and the bicycle has to *want* to change. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Character semantics for filenames (was: win32 reading wide filenames (unicode))
    ... DO WITH CHARACTERS ABOVE "\xFF". ... suspect, openworks on the supplied byte stream AS IS, discregarding ... Unocode inserts hints in strings. ... encoding to perl strings by readdir and from perl strings to the OS ...
    (comp.lang.perl.misc)