I have an XML Document that I write from vb.net to my filesystem successfully.... Now I'd like to have a button that browses for and then opens & imports the data from XML back into a datalistview...
I have the open file dialog working, and can read specific 'static' data from the file, but dynamic amounts of data seems to be a bit of trouble. Could anyone help me out please?
Here's what I have thus far:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--Markbook data file.-->
<Root>
<Metadata>
<Author>first.last</Author>
<WriteTime>19/06/2012 9:23:28 AM</WriteTime>
</Metadata>
<Data>
<StudentRecord>
<Student>student1</Student>
<Mark>88</Mark>
<Grade>Distinction</Grade>
</StudentRecord>
<StudentRecord>
<Student>student2</Student>
<Mark>12</Mark>
<Grade>Participation</Grade>
</StudentRecord>
</Data>
</Root>
How would I go about adding each element of data in the element into a multicolumn datalistview.
There are number of methods/ways to read/parse the XML
document in .net framework but I'd like to suggest Linq-Xml
.
Have a look at this code snippet:
Dim doc = XDocument.Load("x:\folder\file.xml")
Dim result = From ele In doc.Root.Descendants("StudentRecord")
Select New With
{
.Name = ele.Element("Student").Value,
.Mark = ele.Element("Mark").Value,
.Grade = ele.Element("Grade").Value
}
'Bind the List to DataGridView or other bindable control
DataGridView1.DataSource = result.ToList()