0

The title says it all, I want to know how to read various pieces of data from .xml files in visual basic. My current, basic, xml file looks like:

 <?xml version="1.0"?>

-<ArrayOfActivity xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


 -<activity>

 <memno>1239</memno>

 <curweek>0</curweek>

 <rundist>0</rundist>

 <runtime>0</runtime>

 <swimdist>0</swimdist>

 <swimtime>0</swimtime>

 <cycdist>0</cycdist>

 <cyctime>0</cyctime>

 </activity>


 -<activity>

 <memno>1293</memno>

 <curweek>0</curweek>

 <rundist>0</rundist>

 <runtime>0</runtime>

 <swimdist>0</swimdist>

 <swimtime>0</swimtime>

 <cycdist>0</cycdist>

 <cyctime>0</cyctime>
 </activity>
 </ArrayOfActivity>

I want to just read to values from, say, member number 1239, how do I go about this? I cannot make sense of any on-line help, I am fairly new to visual basic. I also save through this method:

 Public Sub SaveDataAct()
    If System.IO.File.Exists("e:\Test\test2.xml") = True Then
        System.IO.File.Delete("e:\Test\test2.xml")
    End If
    Dim serializer As New Xml.Serialization.XmlSerializer(GetType(List(Of activity)))
    Dim fs As New IO.FileStream("e:\Test\test2.xml", IO.FileMode.Create)
    serializer.Serialize(fs, dataentry)
    fs.Close()
End Sub
ryan2405
  • 63
  • 1
  • 6
  • Post the xml as text using the icon with two arrows <> instead of a picture. – jdweng Jan 31 '16 at 22:28
  • This is vb.net not vba – Jules Jan 31 '16 at 22:40
  • Thanks for the formatting help – ryan2405 Jan 31 '16 at 22:45
  • I would recommend using Linq to Xml instead of XmlSerializer - it's more lightweight and allows navigating the Xml. This [thread](http://stackoverflow.com/questions/1066213/linq-to-xml-in-vb-net) contains some simple examples that should help you get started. – Pawel Feb 01 '16 at 05:03

2 Answers2

1

Like already mentioned by Pawel you should rather use Linq to Xml.

Your sample program would look like:

Imports System.Linq
...

Sub Main()
    Dim input As XElement = XElement.Load("e:\Test\test2.xml") 

    //Query your Xml with Linq, almost similiar to Sql. 
    //You can extend with any where/join/group/order by clause
    Dim output = From activies In input...<activity>
                 Where Integer.Parse(activies.<memno>.Value) <= 1293
                 Select activies

    //Output is an IEnumarable of XElement
    For Each activity In output
        Console.WriteLine("Member: {0}", activity.<memno>.Value)
        Console.WriteLine("Current Week: {0}", activity.<curweek>.Value)
        //...
    Next

End Sub

As you can see you can navigate directly thru an XElement with .<ChildElementName> and with ...<DescendantElementName> which is a pretty nice VB .NET feature.

More about LINQ

Alex B.
  • 2,145
  • 1
  • 16
  • 24
1

I would read it to a dataset like this:

 Dim XML_Read as DataSet = new DataSet
 XML_Read.ReadXML("e:\Test\test2.xml")

Then you will get access to the element you want this way:

 XML_Read.tables("activity").Rows(0).Item("memno")
Ehsan
  • 767
  • 7
  • 18