how can I convert (char)02 into UTF8 string

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
D

Daniel Rimmelzwaan

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.
 
S

Stephen Ahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top