473,545 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLDocument character encoding

We are encoding strings using XMLElement:

private string XMLEncode(strin g val)
{
if(val.Length == 0)
return string.Empty;
XmlElement element = xmldoc.CreateEl ement("E");
element.InnerTe xt = val;

return element.InnerXm l;
}

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(strin g val)
{
if(val.Length == 0)
return string.Empty;
element = xmldoc.CreateEl ement("E");
element.InnerTe xt = val;

string temp = element.InnerXm l;
byte[] wrong = System.Text.Enc oding.Unicode.G etBytes(temp);
byte[] right = System.Text.Enc oding.Convert(E ncoding.Unicode ,
Encoding.GetEnc oding("ISO-8859-1"), wrong);
string done = Encoding.GetEnc oding("ISO-8859-1").GetString(r ight);

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 18038
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.in sight.com> wrote in message
news:Ov******** ******@TK2MSFTN GP11.phx.gbl...
We are encoding strings using XMLElement:

private string XMLEncode(strin g val)
{
if(val.Length == 0)
return string.Empty;
XmlElement element = xmldoc.CreateEl ement("E");
element.InnerTe xt = val;

return element.InnerXm l;
}

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(strin g val)
{
if(val.Length == 0)
return string.Empty;
element = xmldoc.CreateEl ement("E");
element.InnerTe xt = val;

string temp = element.InnerXm l;
byte[] wrong = System.Text.Enc oding.Unicode.G etBytes(temp);
byte[] right = System.Text.Enc oding.Convert(E ncoding.Unicode ,
Encoding.GetEnc oding("ISO-8859-1"), wrong);
string done = Encoding.GetEnc oding("ISO-8859-1").GetString(r ight);

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.m icrosoft.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.in sight.com> wrote in message
news:Ov******** ******@TK2MSFTN GP11.phx.gbl...
We are encoding strings using XMLElement:

private string XMLEncode(strin g val)
{
if(val.Length == 0)
return string.Empty;
XmlElement element = xmldoc.CreateEl ement("E");
element.InnerTe xt = val;

return element.InnerXm l;
}

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(strin g val)
{
if(val.Length == 0)
return string.Empty;
element = xmldoc.CreateEl ement("E");
element.InnerTe xt = val;

string temp = element.InnerXm l;
byte[] wrong = System.Text.Enc oding.Unicode.G etBytes(temp);
byte[] right = System.Text.Enc oding.Convert(E ncoding.Unicode ,
Encoding.GetEnc oding("ISO-8859-1"), wrong);
string done = Encoding.GetEnc oding("ISO-8859-1").GetString(r ight);

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
6802
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. The problem is that I'm getting multiple processing instructions and multiple root elements in my xml file when I use a FileStream. Here is an...
3
3765
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>"); **results**
2
1511
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
4163
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 XmlDocument.ReadNode(). The problem is that it's breaking and he doesn't understand why. I didn't exactly either, which is why I'm posting a question here. First,...
4
13641
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
1709
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 New System.IO.MemoryStream
10
13921
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 one below, output from google map API), it always said invalid character at position 229, which I believe is the "ß" character. Can anyone point...
4
5917
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 see? lowercase UTF)
2
4435
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, position 32." I understand that this is becasue I have one apostrophe in the RSS Feed. I have tried few tricks to get rid of apostrophe but of no...
0
7496
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...
0
7428
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...
0
7685
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. ...
0
7941
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6014
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...
1
5354
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...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1039
muto222
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.