-
May 7th, 2009, 03:34 AM
#1
Problem while convert a dataset to xml
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.
-
May 7th, 2009, 08:01 AM
#2
Re: Problem while convert a dataset to xml
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);
-
May 7th, 2009, 10:31 PM
#3
Re: Problem while convert a dataset to xml
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).
-
May 11th, 2009, 09:48 PM
#4
Re: Problem while convert a dataset to xml
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];
-
May 11th, 2009, 11:04 PM
#5
Re: Problem while convert a dataset to xml
Originally Posted by adatapost
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.
-
May 13th, 2009, 10:02 PM
#6
Re: Problem while convert a dataset to xml
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).
-
May 3rd, 2010, 03:54 AM
#7
Re: Problem while convert a dataset to xml
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/Con...eversa-434.php
-
June 1st, 2010, 09:10 PM
#8
Re: Problem while convert a dataset to xml
Originally Posted by adatapost
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|