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?)
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).
Re: best way to get xml into a dataset...
Quote:
Originally Posted by
cicatrix
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)
Re: best way to get xml into a dataset...
Ok didn't know you could ReadXml using a string :D 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.
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.
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:
Dim Data As String = GetData(whats, action, sub_action)
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(whats & ".xml")
objReader.Write(Data)
objReader.Close()
' create a table named whats and read the saved file into it
Dim table As New DataTable(whats)
table.ReadXml(whats & ".xml")
Dim form As New Form
' put a dgv on the form and dock it
form.Show()
Dim DGV As New DataGridView
DGV.Dock = DockStyle.Fill
form.Controls.Add(DGV)
DGV.DataSource = whats
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
p.s. i tried using XmlWriter but it had an issue with the xml too, StreamWriter works though anyway ;)