Results 1 to 5 of 5

Thread: xml serilaization

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    xml serilaization

    Hello, in my program i allow users to create templates which is entered into a datagridview. and i guess the best approach to save it is to serialize the datatable into a string and save it to an SQL xml datafield. is this achievable w/out having to write into an external file. thanks

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: xml serilaization

    Hi,

    If I understand your question correctly then the simple answer is No. If you want to use stored information in your project, which in not hardcoded, then this stored information has to come from an external data file in one form or another.

    In the case of XML, if you bind your DataGridView to a DataSet/DataTable you can then use the WriteXML method of those objects to save your information. Have a look here:-

    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx

    Hope that helps.

    Cheers,

    Ian

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: xml serilaization

    Here's an example of serializing a DataTable to a string.
    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            'create table
            Dim testDataTable As New DataTable("MyTable")
            testDataTable.Columns.Add("SomeColumn", GetType(String))
    
            'populate table
            testDataTable.Rows.Add(New Object() {"some data"})
    
            'write xml to memory stream
            Dim stream As New IO.MemoryStream
            testDataTable.WriteXml(stream)
    
            'convert memory stream into string
            Dim xmlString = ConvertStreamToString(stream)
    
        End Sub
    
        Private Function ConvertStreamToString(stream As IO.MemoryStream) As String
    
            stream.Position = 0
            Using reader = New IO.StreamReader(stream)
                Return reader.ReadToEnd
            End Using
    
        End Function
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: xml serilaization

    If you want to use stored information in your project, which in not hardcoded, then this stored information has to come from an external data file in one form or another.
    Er .... you might want to rethink that a bit!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: xml serilaization

    The question was if the data could be stored in the database w/o having to use an intermediary file for the XML ... yes... by using the stream as wild_bill has shown...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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