how can I convert (char)02 into UTF8 string

  • Thread starter Thread starter Daniel Rimmelzwaan
  • Start date Start date
D

Daniel Rimmelzwaan

Does anyone know how to convert a (char)02 into a UTF8 string that I can put
into an XML element? I've tried all examples that I could find in Visual
Studio help or in google, but I can't get it to work. The one that gives me
the answer will just make my day :).

Thanks,
Daniel.
 
Daniel,

I thought that if you specify the encoding on the XmlDocument class, and
then just add the text in string form, it will perform the encoding for you
automatically?
 
That's what we did, but when we create the element, we get an error saying
something about a special character not being allowed. We've since just
gotten rid of the characters (it was actually (char)02 and (char03) as start
and end delimiters of a status message from an external app), but I'd still
like to know how this works.

Nicholas Paldino said:
Daniel,

I thought that if you specify the encoding on the XmlDocument class,
and then just add the text in string form, it will perform the encoding
for you automatically?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Daniel Rimmelzwaan said:
Does anyone know how to convert a (char)02 into a UTF8 string that I can
put into an XML element? I've tried all examples that I could find in
Visual Studio help or in google, but I can't get it to work. The one that
gives me the answer will just make my day :).

Thanks,
Daniel.
 
Daniel,

Did you try something like :

==
private void button1_Click(object sender, System.EventArgs e)
{
string orig = "\u0002";
XmlTextWriter w = new XmlTextWriter("c:\\test.xml",
new System.Text.UTF8Encoding(false, true));

w.WriteElementString("elementX", orig);
w.Close();

XmlTextReader r = new XmlTextReader("c:\\test.xml");
string str2 = r.ReadElementString("elementX");

if (str2 == orig)
MessageBox.Show("equal");
else
MessageBox.Show("not equal");

r.Close();
}
==

The above code, when run, displays : "equal".
Is this what you are trying to do ?

Stephen
 
Back
Top