473,480 Members | 5,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XMLDocument character encoding

We are encoding strings using XMLElement:

private string XMLEncode(string val)
{
if(val.Length == 0)
return string.Empty;
XmlElement element = xmldoc.CreateElement("E");
element.InnerText = val;

return element.InnerXml;
}

The question is what encoding is being used to translate the string? What is
the default encoding for XMLDocument? Is the resultant string in UTF-8,
Unicode, etc...?
Our server side components require ISO-8859-1 so I am now trying to convert
from one char set to another like this:

private string XMLEncode(string val)
{
if(val.Length == 0)
return string.Empty;
element = xmldoc.CreateElement("E");
element.InnerText = val;

string temp = element.InnerXml;
byte[] wrong = System.Text.Encoding.Unicode.GetBytes(temp);
byte[] right = System.Text.Encoding.Convert(Encoding.Unicode,
Encoding.GetEncoding("ISO-8859-1"), wrong);
string done = Encoding.GetEncoding("ISO-8859-1").GetString(right);

return done;
}

Am I correct in assuming that the encoder is Unicode? Is this a time bomb?
It appears to be working correctly - just seems like a hack!

Thanks;
-Eric
Nov 12 '05 #1
2 18028
The encoding of all strings in the .NET Framework is UTF-16. This means that
your XMLEncode method is returning a UTF-16 string. The correct thing to do
in this case would be to save the XmlDocument to a stream and specify the
encoding you want on the stream writer.

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Eric Cadwell" <ec******@ns.insight.com> wrote in message
news:Ov**************@TK2MSFTNGP11.phx.gbl...
We are encoding strings using XMLElement:

private string XMLEncode(string val)
{
if(val.Length == 0)
return string.Empty;
XmlElement element = xmldoc.CreateElement("E");
element.InnerText = val;

return element.InnerXml;
}

The question is what encoding is being used to translate the string? What is the default encoding for XMLDocument? Is the resultant string in UTF-8,
Unicode, etc...?
Our server side components require ISO-8859-1 so I am now trying to convert from one char set to another like this:

private string XMLEncode(string val)
{
if(val.Length == 0)
return string.Empty;
element = xmldoc.CreateElement("E");
element.InnerText = val;

string temp = element.InnerXml;
byte[] wrong = System.Text.Encoding.Unicode.GetBytes(temp);
byte[] right = System.Text.Encoding.Convert(Encoding.Unicode,
Encoding.GetEncoding("ISO-8859-1"), wrong);
string done = Encoding.GetEncoding("ISO-8859-1").GetString(right);

return done;
}

Am I correct in assuming that the encoder is Unicode? Is this a time bomb?
It appears to be working correctly - just seems like a hack!

Thanks;
-Eric

Nov 12 '05 #2
Thanks.
-Eric
"Dare Obasanjo [MSFT]" <da***@online.microsoft.com> wrote in message
news:40********@news.microsoft.com...
The encoding of all strings in the .NET Framework is UTF-16. This means that your XMLEncode method is returning a UTF-16 string. The correct thing to do in this case would be to save the XmlDocument to a stream and specify the
encoding you want on the stream writer.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Eric Cadwell" <ec******@ns.insight.com> wrote in message
news:Ov**************@TK2MSFTNGP11.phx.gbl...
We are encoding strings using XMLElement:

private string XMLEncode(string val)
{
if(val.Length == 0)
return string.Empty;
XmlElement element = xmldoc.CreateElement("E");
element.InnerText = val;

return element.InnerXml;
}

The question is what encoding is being used to translate the string? What
is
the default encoding for XMLDocument? Is the resultant string in UTF-8,
Unicode, etc...?
Our server side components require ISO-8859-1 so I am now trying to

convert
from one char set to another like this:

private string XMLEncode(string val)
{
if(val.Length == 0)
return string.Empty;
element = xmldoc.CreateElement("E");
element.InnerText = val;

string temp = element.InnerXml;
byte[] wrong = System.Text.Encoding.Unicode.GetBytes(temp);
byte[] right = System.Text.Encoding.Convert(Encoding.Unicode,
Encoding.GetEncoding("ISO-8859-1"), wrong);
string done = Encoding.GetEncoding("ISO-8859-1").GetString(right);

return done;
}

Am I correct in assuming that the encoder is Unicode? Is this a time

bomb? It appears to be working correctly - just seems like a hack!

Thanks;
-Eric


Nov 12 '05 #3

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

Similar topics

1
6793
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
3
3763
by: todd | last post by:
Simply trying to load xml into a DOM without the dom converting my escape sequence. **code snippet** XmlDocument xmlDoc = new XmlDocument() ; xmlDoc.LoadXml("<x>hello world</x>"); ...
2
1506
by: Adam Smith | last post by:
Hello, I have a file: test.txt: <?xml version="1.0" encoding="UTF-8"?> <First> <value>3</value> </First> I am reading it like this:
7
4152
by: Mark | last post by:
Hi... A colleague just referred this question to me. He's getting an xml file from another party, which he's trying to process into another dom using an XmlTextReader and...
4
13613
by: Saurabh Sharma | last post by:
Hi I am making a XmlDocument . How can i check the Size of the file size of XmlDocument when i will save it to the disk. Regards Saurabh Sharma
3
1701
by: almurph | last post by:
Hi everyone, Hope you can help me. I'm converting an XMlDocuemnt object to a string. I use the following code: Dim doc As New XmlDocument 'Convert XmlDocument object -> String Dim ms As...
10
13906
by: lamxing | last post by:
Dear all, I've spent a long time to try to get the xmldocument.load method to handle UTF-8 characters, but no luck. Every time it loads a document contains european characters (such as the...
4
5915
by: MaxMax | last post by:
A question: all the XML files I've seen use this declaration: <?xml version="1.0" encoding="UTF-8"?> BUT files created using XmlDocument have: <?xml version="1.0" encoding="utf-8"?> (you...
2
4424
by: Manikrag | last post by:
Hi All, I am getting error while am loading RSS Stream in XMLDocument object. The error is as follows. "System.Xml.XmlException: '', hexadecimal value 0x19, is an invalid character. Line 18,...
0
7039
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,...
0
6904
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...
0
7037
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,...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
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...
1
4770
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...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.