Home All Groups Group Topic Archive Search About

XMLWriter Anyway to produce Doc with no encoding Info>

Author
16 Mar 2006 8:03 PM
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\" ?>

Author
16 Mar 2006 11:55 PM
Jon Skeet [C# MVP]
Bob <b**@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
Are all your drivers up to date? click for free checkup

Author
17 Mar 2006 7:32 AM
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



Show quoteHide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1e840b579aabf2ce98cf6a@msnews.microsoft.com...
> Bob <b**@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
Author
17 Mar 2006 8:14 AM
Jon Skeet [C# MVP]
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
Author
17 Mar 2006 7:37 PM
Bob
Hi Jon,
Thank you for the examples.
I'll implement them and have a play around.
much appreciated.
Regards
Bob
Show quoteHide quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:1142583262.683198.170010@z34g2000cwc.googlegroups.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.ToArray()));
>     }
> }
>
> public class NullEncodingStreamWriter : StreamWriter
> {
>     public override Encoding Encoding
>     {
>         get
>         {
>             return null;
>         }
>     }
>
>     public NullEncodingStreamWriter (Stream stream) : base (stream)
>     {
>     }
> }
>
> Jon
>

Bookmark and Share