469,336 Members | 5,230 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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 17818
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by todd | last post: by
2 posts views Thread by Adam Smith | last post: by
4 posts views Thread by Saurabh Sharma | last post: by
4 posts views Thread by MaxMax | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.