473,749 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(m yMemoryStream). 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.E ncoding 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 2490
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(m yMemoryStream). 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(m yMemoryStream);
reader.MoveToCo ntent();
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(m yMemoryStream);
reader.MoveToCo ntent();
string xml = reader.ReadOute rXml();
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...@yaho o.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(m yMemoryStream). 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(m yMemoryStream);
reader.MoveToCo ntent();
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(m yMemoryStream);
reader.MoveToCo ntent();
string xml = reader.ReadOute rXml();
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
11976
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 convert between data types, but was thinking that this isn't really necessary since I can access the memory directly in c. I want to treat the text as native integer so that I can perform fast bit-level manipulations. This code works fine...but I'm...
3
5141
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 starting at position 0. I thought that char was 2 bytes, but this makes it seem as though it is just 1 when I write to a file. Why is this? I imagine the extra bit is just a null bit (correct me if I'm wrong). I don't know if this helps but when I...
7
5638
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 strings of any lengths into an array of pointers to char type variable. my problem is: given the declaration
16
7535
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 receiving 34,14,63,51,0,0 from the port as I connected Tx and Rx pins to each other by using the following code: private void com_DataReceived(object sender, SerialDataReceivedEventArgs e) {
15
435
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; for(int i=0; i<line->get_length(); i++) {
9
3131
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
2162
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 address with the label MY_AREA. I mean, the char array looks like "char my_area;", and how to use the label MY_AREA and THE_SIZE_OF_THIS_AREA to create pointer like my_area? Thanx!!
2
2818
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 usage) , but I'm not sure if it's being caused by the following: void example() { char array=""; }
11
7706
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
3794
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);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.