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

Find the encoding of an XML char array (or stream) stored in memory

Hi,

I have an application that involves sending a lot of XML data to
various places. The problem is that once in a while, I just want the
XML document as a string (for example, sending to a typed dataset
tableadaptor). In that case, I have a problem: what encoding do I use
to conver the byte[] (or memorystream) into a string? The encoding is
in the file, of course, like any XML file, but how do I find out
which? I tried using an XmlTextReader - this is supposed to have an
Encoding... but all I did basically was

myEncoding = new XmlTextReader(myMemoryStream).Encoding;

and in that case it's null. Is there something I have to do with the
textreareader to get it to populate it's own "Encoding" field from the
given text? The presence of a null indicates I'm doing something
really wierd, since the documentation for XmlTextReader.Encoding says:

Property Value
The encoding value. If no encoding attribute exists, and there is no
byte-order mark, this defaults to UTF-8.

Mar 2 '07 #1
3 2464
Martin Z wrote:
I have an application that involves sending a lot of XML data to
various places. The problem is that once in a while, I just want the
XML document as a string (for example, sending to a typed dataset
tableadaptor). In that case, I have a problem: what encoding do I use
to conver the byte[] (or memorystream) into a string? The encoding is
in the file, of course, like any XML file, but how do I find out
which? I tried using an XmlTextReader - this is supposed to have an
Encoding... but all I did basically was

myEncoding = new XmlTextReader(myMemoryStream).Encoding;

and in that case it's null. Is there something I have to do with the
textreareader to get it to populate it's own "Encoding" field from the
given text?
Yes, the reader has to read the beginning of the stream to find a byte
order mark or look at the XML declaration.
If you do e.g.
XmlTextReader reader = new XmlTextReader(myMemoryStream);
reader.MoveToContent();
then you should be able to get the encoding
Encoding myEncoding = reader.Encoding;

Note that in .NET 2.0 there is also a method ReadOuterXml so instead of
trying to find out the encoding and decode the bytes in the memory
stream to a string it might suffice to do e.g.
XmlTextReader reader = new XmlTextReader(myMemoryStream);
reader.MoveToContent();
string xml = reader.ReadOuterXml();
That will strip out anything like comment or processing instructions
before the root element however. If you want the complete XML then you
might need to call Read and ReadOuterXml for all top level nodes instead
of using MoveToContent.

And most APIs taking XML input usually have overloads to read from a
stream directly, so double check that your API does not take a stream
before you take efforts to decode your stream into a string of XML.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 2 '07 #2
For the theory of how to do this, see Appendix F of the XML spec:

http://www.w3.org/TR/REC-xml/#sec-guessing

Code to implement this isn't particularly difficult to write.
Mar 2 '07 #3
Thankyou, you solved my problem perfectly. And as I said, I was using
a typed dataset tableadaptor on SQL 2000 - those use strings for any
text field, so I needed it as a string.

On Mar 2, 10:16 am, Martin Honnen <mahotr...@yahoo.dewrote:
Martin Z wrote:
I have an application that involves sending a lot of XML data to
various places. The problem is that once in a while, I just want the
XML document as a string (for example, sending to a typed dataset
tableadaptor). In that case, I have a problem: what encoding do I use
to conver the byte[] (or memorystream) into a string? The encoding is
in the file, of course, like any XML file, but how do I find out
which? I tried using an XmlTextReader - this is supposed to have an
Encoding... but all I did basically was
myEncoding = new XmlTextReader(myMemoryStream).Encoding;
and in that case it's null. Is there something I have to do with the
textreareader to get it to populate it's own "Encoding" field from the
given text?

Yes, the reader has to read the beginning of the stream to find a byte
order mark or look at the XML declaration.
If you do e.g.
XmlTextReader reader = new XmlTextReader(myMemoryStream);
reader.MoveToContent();
then you should be able to get the encoding
Encoding myEncoding = reader.Encoding;

Note that in .NET 2.0 there is also a method ReadOuterXml so instead of
trying to find out the encoding and decode the bytes in the memory
stream to a string it might suffice to do e.g.
XmlTextReader reader = new XmlTextReader(myMemoryStream);
reader.MoveToContent();
string xml = reader.ReadOuterXml();
That will strip out anything like comment or processing instructions
before the root element however. If you want the complete XML then you
might need to call Read and ReadOuterXml for all top level nodes instead
of using MoveToContent.

And most APIs taking XML input usually have overloads to read from a
stream directly, so double check that your API does not take a stream
before you take efforts to decode your stream into a string of XML.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 2 '07 #4

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

Similar topics

4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
3
by: Mark Miller | last post by:
I have a char array and when I write it to a file using BinaryWriter the position of the pointer is the size of the array + 1. For example: writing char leaves the pointer at position 26 after...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
16
by: halukg | last post by:
I am trying to send a 6 byte char array from the serial port in new C# 2005 Express: com.Write(new string(new char { (char)34, (char)14, (char)192, (char)51, (char)0, (char)0 }, 0, 6)); I am...
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; ...
9
by: Angus | last post by:
Hello I want to write a function which returns a char array. But if I write this: char myfunction() { return "123"; }
7
by: s88 | last post by:
Hi all: I have a memory area fill will 0x90(NOP). This area is allocated by the linker. I want to transfer this area into a char array. I have already known the size of this area and the beginning...
2
by: ivan47 | last post by:
I'm creating a char array in the following way, and don't know whether it is being collected or not. I know I have a memory leak somewhere in my code ("ps wwvg | grep myapplication" shows memory...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.