PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 best way to get xml into a dataset...-VBForums
Results 1 to 6 of 6

Thread: best way to get xml into a dataset...

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    best way to get xml into a dataset...

    Hi all, just wondering if what i've been doing for the last few months is the best method to get data from xml and onto datatables within a dataset or if there is a simpler way i have completely overlooked. Rather than go into the code i will explain the process step by step.

    1/ setup a dataset and add tables with columns ready for data
    2/ setup various classes so i can construct a list (or array) of a specific type
    3/ get the xml into an XElement type.
    4/ use LINQ to obtain a query from the xml
    5/ use this query to create lines of the type we setup in step 2/
    6/ use these lines to create a datarow and add to the relevent table.
    7/ bind the table to a dgv to show the table.

    Now i am wondering if i can say for the type in step 2/ use DataRow as the type thus missing out step 5/ ? is this possible? what other ways are there to get xml into a DataSet? One other method that occurs to me is to load the xml directly into the datatable (this is possible?) but i am not sure what the xml needs to look like for that to work. My xml is simple it contains only values and no parameters if that helps...

    I require my final dgv to be sortable by column clicks etc so needs to comply to various interfaces, any pointers on this (perhaps i can avoid using a dataset and populate direct from my DataObjects so i only need steps 2, 3, 4, 5 and 7?)
    Last edited by Megalith; Apr 21st, 2010 at 04:30 AM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: best way to get xml into a dataset...

    Before we get into comparison of many ways to get data from the xml file tell me, have you considered dataset's own methods called .ReadXML and ReadXMLSchema (also .WriteXML and .WriteXMLSchema)?

    (The same methods exist for Datatable also).

  3. #3

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: best way to get xml into a dataset...

    Quote Originally Posted by cicatrix View Post
    Before we get into comparison of many ways to get data from the xml file tell me, have you considered dataset's own methods called .ReadXML and ReadXMLSchema (also .WriteXML and .WriteXMLSchema)?

    (The same methods exist for Datatable also).
    I was thinking of that method, not sure what the data needs to look like for it to work, am thinking that directly editing the xml to match may be simpler than the above processes. I would have to read the xml and save it locally prior to doing this as it isn't simply a case of pointing somewhere and getting the xml (the xml is generated on the site and you need to register to obtain it which my code does)
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  4. #4

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: best way to get xml into a dataset...

    Ok didn't know you could ReadXml using a string this looks good, will get stuck in and get back if i have issues.


    EDIT lol ok the string is the filename save it first then.
    Last edited by Megalith; Apr 21st, 2010 at 06:44 AM.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: best way to get xml into a dataset...

    Theoretically ReadXML can read any XML, but if you're using a particular schema I'd saved it with WriteXMLSchema first. This way you will have the file structure that will guaranteedly be read by the ReadXML method and you can edit it afterwards. To put it simpler - first create a XML with WriteXML and then edit it if needed.

  6. #6

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: best way to get xml into a dataset...

    Hey again Cicatrix, just been trying it and yep it didn't load my saved file as it contained no schema and couldn't infer it correctly and so generated an error. Am now starting work on creating the schema as a string and saving it along with the data and then read it to the table with ReadXML. let you know how it goes but this is what i have so far
    vb.net Code:
    1. Dim Data As String = GetData(whats, action, sub_action)
    2.             Dim objReader As StreamWriter
    3.             Try
    4.                 objReader = New StreamWriter(whats & ".xml")
    5.                 objReader.Write(Data)
    6.                 objReader.Close()
    7.                 ' create a table named whats and read the saved file into it
    8.                 Dim table As New DataTable(whats)
    9.                 table.ReadXml(whats & ".xml")
    10.                 Dim form As New Form
    11.                 ' put a dgv on the form and dock it
    12.                 form.Show()
    13.                 Dim DGV As New DataGridView
    14.                 DGV.Dock = DockStyle.Fill
    15.                 form.Controls.Add(DGV)
    16.                 DGV.DataSource = whats
    17.             Catch Ex As Exception
    18.                 MsgBox(Ex.Message)
    19.             End Try
    p.s. i tried using XmlWriter but it had an issue with the xml too, StreamWriter works though anyway
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width