Click to See Complete Forum and Search --> : Problem while convert a dataset to xml


jinho929
May 7th, 2009, 04:34 AM
I have a simple snippet code like this:

DbDataAdapter da = _dbProvider.CreateDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
MemoryStream stream = new MemoryStream();
ds.WriteXml(stream);
StreamReader reader = new StreamReader(stream);
stream.Position = 0;
string s = reader.ReadToEnd();


After filling data to dataset, ds has a table, table has 1 row, this row has many fields. there's 1 field its type is byte[128] (all items' value = 0).
String s has many tags, the data of the tag correspond to above field is "AAAA...AAA="

My purpose is to convert back string "AAAA...AAA=" to an array byte[128]
Can anybody explain me this problem and show me an solution to achieve my purpose.

Thank you very much.

adatapost
May 7th, 2009, 09:01 AM
Use System.Text.Encoding.ASCII or System.Text.Encoding.Unicode class to convert string into byte array and vice versa.

....
stream.Position = 0;
string s = reader.ReadToEnd();
// String to byte array
byte[]b= System.Text.Encoding.ASCII.GetBytes(s);

jinho929
May 7th, 2009, 11:31 PM
The problem here is string correspond to the array is "AAA..A=" (length = 172).
After convert string back to byte[] like:
byte[] b = System.Text.Encoding.ASCII.GetBytes(s);
I have an array (length = 172) with b[1], b[2], ...b[170] = 65, b[171] = 61
while the original array just have 128 items (all items' value = 0).

adatapost
May 11th, 2009, 10:48 PM
I think you want to create an array of content not an entire xml doc.

....
DataSet ds = new DataSet();
da.Fill(ds);
// This way you got an array of bytes
byte []b=(byte [])ds.Tables[0][0][0];

jinho929
May 12th, 2009, 12:04 AM
I think you want to create an array of content not an entire xml doc.

....
DataSet ds = new DataSet();
da.Fill(ds);
// This way you got an array of bytes
byte []b=(byte [])ds.Tables[0][0][0];

Because this function is in a webservice, hence I must transform dataset to a string(xml) and then return to client like this:

MemoryStream stream = new MemoryStream();
ds.WriteXml(stream);
StreamReader reader = new StreamReader(stream);
stream.Position = 0;
string s = reader.ReadToEnd();

On client, I got the xml but content of the tag correspond to the byte array in dataset is so strange. Convert back this string I got an byte array, but this one is not the same with the original byte array in dataset.

adatapost
May 13th, 2009, 11:02 PM
Dear jinho929,

If web-service client is .NET then you are free to use DataSet class directly.

For example,

YourWSProxyClass c=new YourWSProxyClass();
DataSet ds=c.getData(); // Assume the getData() is WebService Method, if getData Returns
// DataSet (Serialized).

elizas
May 3rd, 2010, 04:54 AM
XML is a simple and flexible system for defining data formats. This is completely platform independent and adopted everywhere for representing complex documents and data structures. For data transmission on web, its having significant contribution.

It is very common to use dataset in .NET applications. A dataset may contain a single table or multiple tables that are related though primery and foreign keys. We can also use XML to completely reconstruct the dataset. Now days it’s been the major use of XML to store both rowset (single table) and hierarchical (multiple-table) data in it.


http://www.mindfiresolutions.com/Converting-Datatable-to-xml-string-and-viceversa-434.php

larryp7639
June 1st, 2010, 10:10 PM
Use System.Text.Encoding.ASCII or System.Text.Encoding.Unicode class to convert string into byte array and vice versa.

....
stream.Position = 0;
string s = reader.ReadToEnd();
// String to byte array
byte[]b= System.Text.Encoding.ASCII.GetBytes(s);


I also think so.

__________________
Watch Splice Online Free (http://moviesonlinefree.biz)