tags:

views:

74

answers:

3

// .net 2.0 and vs2005 used.

I find some code below. I am not sure I can extended the sample code or not? thank you.

if (radioButton.Checked)
{
    MemoryStream ms=new MemoryStream();
    byte[] data=ASCIIEncoding.ASCII.GetBytes(textBox1.Text);
    ms.Write(data,0,data.Length);
    reader = new XmlTextReader(ms);
    //some procesing code
    ms.Close();
    reader.Close();
}

BTW, Could you please help me to do some dissection about the line below.

byte[] data=ASCIIEncoding.ASCII.GetBytes(textBox1.Text);
+4  A: 
using (var reader = new StringReader(textBox1.Text))
using (var xmlReader = XmlReader.Create(reader))
{
    while (xmlReader.Read())
    {
        //some procesing code
    }
}

Concerning this line byte[] data=ASCIIEncoding.ASCII.GetBytes(textBox1.Text);:

declare a variable called data of type array of byte and assign it to the contents of textBox1.Text string converted to an array of byte using ASCII encoding.

Darin Dimitrov
@Darin Dimitrov, I looked up MSN for keyword of **var** : "Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. ". And I still using .net 2.0 in my case. thank you.
Nano HE
Then replace the two `vars` in my code with `TextReader` and `XmlReader` respectively.
Darin Dimitrov
+1  A: 

To answer your second question:

byte[] data=ASCIIEncoding.ASCII.GetBytes(textBox1.Text);

Normally, text is stored as a sequence of characters, each of which is stored in a unicode format that takes several bytes per character. The ASCII format is an older format that uses one byte (in fact, originally 7 bits, and then extended to use 8 bits) per character.

The ASCIIEncoding provides a way of converting to or from this old format. The above line of code converts the text into an ASCII representation, and returns a "raw" array of bytes containing the text in that format.

Note that ASCII uses a byte for each character, so can only represent 256 different characters - Unicode can represent thousands of characters, so during the conversion, some characters will be lost (converted to a simpler form where acute/cedilla/umlaut is lost, or replaced by another character - e.g. "?")

(Typical advantages of doing this are that it may be easier to write code to process the simpler format, or that it uses less memory as it only uses one byte per character, or that you need to send the data to a "third party" that requires it to be ASCII)

Jason Williams
Thanks for your details. Helped a lot.
Nano HE
+1  A: 

Filestream fs = new Filestream(textBox1.Text); XmlReader reader = XmlReader.Create(fs);

From XmlReader msdn documentation.

Nick Miller
Hi Nick, Thank you so much.
Nano HE