Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Tuesday, June 29, 2010

Binary to String Conversion in C#

Converting binary to different format is quiet common scenario for developers.Its just two line of code that will do the magic , but when time matter love search in google so if this blog help someone really happy for that.
Binary to String

System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string str = enc.GetString(MyByteArray);
if you want to make sure that there is no data loss then use Base64 Encoding

Base64 Encoding :- converts binary data to plain text using 64 case-sensitive, printable ASCII characters: A-Z, a-z, 0-9, plus sign (+) and forward slash (/), and may be terminated with 0-2 "padding" characters represented by the equal sign (=).

Convert.ToBase64String(myByteArray);
when you are working with picture it will be good to use Base64 conversion so you could make sure that there is no data lose

Monday, June 21, 2010

Loading xml from string

XML Document Approach

Let see how we can load xml from a string to a xmldocument
object
Step 1 Create XmlDocument Object
XmlDocument doc = new
XmlDocument();
Step 2
Creat a StringReader Object
System.IO.StringReader sr = new
System.IO.StringReader("XML
Sring");
Step 3 Create
XmlReader Object
XmlReader xmlrd =
XmlReader.Create(sr)

Here we are calling XmlReader.Create method and passing a
StringReader object
Step 4 Load xml
doc.Load(xmlrd);
Uses Load method of
XmlDocument object and pass XmlReader

XDocument Approach
XDocument.Parse("StringXML", LoadOptions.PreserveWhitespace);
In Xdocument you can directly pass the string and specify the load
options.Even you can directly parse into a XElement
XElement.Parse("StringXML")
You could also specify the declaration for your xml when you are creating the XDocument.Look at the following line
XDocument xdoc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "YES"), XElement.Parse("StringXML"));

Here XDeclaration you can specify version of xml,encoding you are using and standalone or not