CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    7

    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.

  2. #2
    Join Date
    Apr 2009
    Location
    Mehsana/Gujarat/India
    Posts
    6

    Smile 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);

  3. #3
    Join Date
    May 2009
    Posts
    7

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

  4. #4
    Join Date
    Apr 2009
    Location
    Mehsana/Gujarat/India
    Posts
    6

    Angry 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];

  5. #5
    Join Date
    May 2009
    Posts
    7

    Re: Problem while convert a dataset to xml

    Quote Originally Posted by adatapost View Post
    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.

  6. #6
    Join Date
    Apr 2009
    Location
    Mehsana/Gujarat/India
    Posts
    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).

  7. #7
    Join Date
    Jan 2010
    Posts
    24

    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

  8. #8
    Join Date
    Jun 2010
    Posts
    1

    Re: Problem while convert a dataset to xml

    Quote Originally Posted by adatapost View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured