Dissappearing text in Byte[] conversion

G

Guest

I am creating an XML file through the XmlTextWriter. This is output to a
MemoryStream which I convert a string through a Byte Array. Everything works
correctly except for one BIG issue. My XML file is being truncated somewhere
in the process. Large XML files give a truncated result, and small ones rsult
in a Byte[].Length = 0 .

I assume the it is getting stuck in a buffer??
I tried Fluch() on the MemStream and Base Stream without success.

Code Fragment:
MemoryStream memStream = new MemoryStream();
xmlw = new XmlTextWriter(memStream,Encoding.UTF8);
//
//Code that creates the XML Document
//xmlw.BaseStream.Flush();
//memStream.Flush();

memStream.Position = 0;
Byte[] info = new byte[memStream.Length];
int li = memStream.Read(info,0,(int)memStream.Length);
int lo = info.Length;
return new System.Text.UTF8Encoding().GetString(info,0,info.Length);
 
J

Jon Skeet [C# MVP]

Ian said:
I am creating an XML file through the XmlTextWriter. This is output to a
MemoryStream which I convert a string through a Byte Array. Everything works
correctly except for one BIG issue. My XML file is being truncated somewhere
in the process. Large XML files give a truncated result, and small ones rsult
in a Byte[].Length = 0 .

I assume the it is getting stuck in a buffer??
I tried Fluch() on the MemStream and Base Stream without success.

Code Fragment:
MemoryStream memStream = new MemoryStream();
xmlw = new XmlTextWriter(memStream,Encoding.UTF8);
//
//Code that creates the XML Document
//xmlw.BaseStream.Flush();
//memStream.Flush();

memStream.Position = 0;
Byte[] info = new byte[memStream.Length];
int li = memStream.Read(info,0,(int)memStream.Length);
int lo = info.Length;
return new System.Text.UTF8Encoding().GetString(info,0,info.Length);

It's not the base stream you need to flush - it's the XmlTextWriter.
You should close that, and then use ToArray on the MemoryStream.

Having said all that - why don't you just use a StringWriter in the
first place?
 
G

Guest

I was using a StringWriter and it worked great except for the fact that it
would default the XML tag to UTF-16. When I would attempt to open this file
in my transform after saving it to disk I would get an encoding error.

According to class documentation IO classes that having a underlying string
default to UTF-16 and you can't change this...well I couldn't get a UTF-8 tag
instead.

any suggestions?
 
J

Jon Skeet [C# MVP]

Ian said:
I was using a StringWriter and it worked great except for the fact that it
would default the XML tag to UTF-16.

There's an easy solution to that:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

public override Encoding Encoding
{
get { return encoding; }
}

}

Create a StringWriterWithEncoding with the UTF-8 encoding, and write to
that instead - it should be fine.
 
G

Guest

I have tried setting the Encoding property before, but I always get "
REad-Only Property" error.

I did the following and got the same result for this.Encoding = encoding;


public class StringWriterUTF8:System.IO.StringWriter
{
//System.Text.Encoding encoding; << what is this for?
public StringWriterUTF8(System.Text.Encoding encoding)
{
this.Encoding = encoding;
}
public override System.Text.Encoding Encoding
{
get
{
return encoding;
}
}

}





:
 
J

Jon Skeet [C# MVP]

Ian said:
I have tried setting the Encoding property before, but I always get "
REad-Only Property" error.

Yes, that's why I provided a class which derives from StringWriter.
I did the following and got the same result for this.Encoding = encoding;

You don't need to set the Encoding property - you provide the value
when you construct the instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top