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

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 18018
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
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
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
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
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
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
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
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
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
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
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.