[RESOLVED] Stripping an XML and writing to a list box.
Hi guys,
I am currently working on a project in where when a Coordinators ID is selected from a combo box, I need to strip an XML file and place the subject code and name in a list box. ("code" - "Name" - "Department number").
I am not overly experienced with Visual Basic so any direction as to how to do this would be really helpful.
The xml file is written as follows. The main problem I am having is the fact the coordinatorsID is after the subect code, I have never dealt with this before.
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<subject>
<SubjCode>AV597</SubjCode>
<SubjectName>VEHICLE AERODYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
<subject>
<SubjCode>AA001</SubjCode>
<SubjectName>VEHICLE DYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
Thanks in advance!
Rob
Re: Stripping an XML and writing to a list box.
You can do this using the System.LINQ namespace. There are examples on MSDN. I have a small piece of code, that's written in C#. You can modify this code to suit your requirement of reading an XML file.
c# Code:
Console.WriteLine("Querying tree loaded with XDocument.Load");
XDocument loaded = XDocument.Load(sourceFile);
var Coordinators = from Coordinator in loaded.Descendants("subject")
select new
{
Position = Coordinator.Element("SubjCode").Value,
CoordinatorId = Coordinator.Element("SubjectName").Value
};
foreach (var c in Coordinators)
{
Console.WriteLine("{0},{1}", c.CoordinatorId, c.Position);
}
Re: Stripping an XML and writing to a list box.
Try this
Code:
Private Sub Stripper()
Dim Data = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<subject>
<SubjCode>AV597</SubjCode>
<SubjectName>VEHICLE AERODYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
<subject>
<SubjCode>AA001</SubjCode>
<SubjectName>VEHICLE DYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
</start>
ListBox1.DataSource = _
( _
From X In Data...<subject> _
Select Name = X.<SubjectName>.Value _
).ToArray
End Sub
Re: Stripping an XML and writing to a list box.
Or this which contats the three items together
Code:
Private Sub Stripper()
Dim Data = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<subject>
<SubjCode>AV597</SubjCode>
<SubjectName>VEHICLE AERODYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
<subject>
<SubjCode>AA001</SubjCode>
<SubjectName>VEHICLE DYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
</start>
ListBox1.DataSource = (From X In Data...<subject> Select _
Line = X.<SubjCode>.Value & " - " & _
X.<SubjectName>.Value & " - " & X.<DeptNum>.Value).ToArray
End Sub
Re: Stripping an XML and writing to a list box.
Quote:
Originally Posted by
kevininstructor
Or this which contats the three items together
Code:
Private Sub Stripper()
Dim Data = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<subject>
<SubjCode>AV597</SubjCode>
<SubjectName>VEHICLE AERODYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
<subject>
<SubjCode>AA001</SubjCode>
<SubjectName>VEHICLE DYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
</start>
ListBox1.DataSource = (From X In Data...<subject> Select _
Line = X.<SubjCode>.Value & " - " & _
X.<SubjectName>.Value & " - " & X.<DeptNum>.Value).ToArray
End Sub
Hey mate, thanks for the help, this only does part of the job. There is more coordinators than just the one and more subjects than just the two. So first need it to read the subjects from an XML file named students, file path is the applcation start up parth \subject.xml.
Secondly need it to filter only the subjects for the selected tutor.
I'm pretty lost, so your help is much appreciated.
-Rob
Re: Stripping an XML and writing to a list box.
Quote:
Originally Posted by
abhijit
You can do this using the System.LINQ namespace. There are examples on MSDN. I have a small piece of code, that's written in C#. You can modify this code to suit your requirement of reading an XML file.
c# Code:
Console.WriteLine("Querying tree loaded with XDocument.Load");
XDocument loaded = XDocument.Load(sourceFile);
var Coordinators = from Coordinator in loaded.Descendants("subject")
select new
{
Position = Coordinator.Element("SubjCode").Value,
CoordinatorId = Coordinator.Element("SubjectName").Value
};
foreach (var c in Coordinators)
{
Console.WriteLine("{0},{1}", c.CoordinatorId, c.Position);
}
Sorry, but I really don't understand this :(
Re: Stripping an XML and writing to a list box.
Re: Stripping an XML and writing to a list box.
Try this...I've only tested this for 1 co-id and multiple subjects.
Code:
Dim coll As New System.Collections.ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objReader As New System.IO.StreamReader("C:\co.xml") ' I'm assuming this is your file path
Dim intFirstPos As Integer
Dim intEndTagPos As Integer
Dim strLine As String
Dim strValue As String
While Not objReader.EndOfStream()
strLine = objReader.ReadLine
intFirstPos = InStr(strLine, "<CoordinatorID>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</CoordinatorID>", CompareMethod.Text)
strValue = Mid(strLine, intFirstPos + Len("<CoordinatorID>"), intEndTagPos - (intFirstPos + Len("<CoordinatorID>")))
If Not ComboBox1.Items.Contains(strValue) Then
ComboBox1.Items.Add(strValue)
End If
End If
End While
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
CreateCollection(ComboBox1.Text)
For Each Str As String In coll
ListBox1.Items.Add(Str)
Next
End Sub
Private Sub CreateCollection(ByVal strCoId As String)
Dim objReader As New System.IO.StreamReader("C:\co.xml")
Dim intFirstPos As Integer
Dim intEndTagPos As Integer
Dim strLine As String
Dim strCoIDValue As String
Dim strSubCodeValue As String
Dim strSubNameValue As String
Dim strDeptNumValue As String
While Not objReader.EndOfStream()
strLine = objReader.ReadLine
If strCoId = strCoIDValue Then
coll.Add(strSubCodeValue & " - " & strSubNameValue & " - " & strDeptNumValue)
strCoIDValue = ""
End If
intFirstPos = InStr(strLine, "<SubjCode>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</SubjCode>", CompareMethod.Text)
strSubCodeValue = Mid(strLine, intFirstPos + Len("<SubjCode>"), intEndTagPos - (intFirstPos + Len("<SubjCode>")))
End If
intFirstPos = InStr(strLine, "<SubjectName>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</SubjectName>", CompareMethod.Text)
strSubNameValue = Mid(strLine, intFirstPos + Len("<SubjectName>"), intEndTagPos - (intFirstPos + Len("<SubjectName>")))
End If
intFirstPos = InStr(strLine, "<DeptNum>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</DeptNum>", CompareMethod.Text)
strDeptNumValue = Mid(strLine, intFirstPos + Len("<DeptNum>"), intEndTagPos - (intFirstPos + Len("<DeptNum>")))
End If
intFirstPos = InStr(strLine, "<CoordinatorID>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</CoordinatorID>", CompareMethod.Text)
strCoIDValue = Mid(strLine, intFirstPos + Len("<CoordinatorID>"), intEndTagPos - (intFirstPos + Len("<CoordinatorID>")))
End If
End While
End Sub
Re: Stripping an XML and writing to a list box.
Perhaps I will get it right this time :)
Load CoordinatorID in to a ComboBox, when selected place "code" - "Name" - "Department number" into a ListBox.
grab data from here, in your case an XML file
Code:
Private Shared Function GetData() As XDocument
Dim Data = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<start xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<subject>
<SubjCode>AV597</SubjCode>
<SubjectName>VEHICLE AERODYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
<subject>
<SubjCode>AA001</SubjCode>
<SubjectName>VEHICLE DYNAMICS</SubjectName>
<DeptNum>116</DeptNum>
<CoordinatorID>C705</CoordinatorID>
</subject>
<subject>
<SubjCode>CC001</SubjCode>
<SubjectName>VEHICLE ENGINES</SubjectName>
<DeptNum>666</DeptNum>
<CoordinatorID>B111</CoordinatorID>
</subject>
</start>
Return Data
End Function
Load ComboBox1 with Distinct CoordinatorID
Code:
Private Sub Form2_Load() Handles MyBase.Load
ComboBox1.DataSource = _
( _
From X In GetData()...<subject> _
Select X.<CoordinatorID>.Value _
Distinct _
).ToArray
End Sub
Populate ListBox when the selection changes on the ComboBox
Code:
Private Sub ComboBox1_SelectedIndexChanged() Handles ComboBox1.SelectedIndexChanged
ListBox1.DataSource = _
( _
From X In GetData()...<subject> _
Where X.<CoordinatorID>.Value = ComboBox1.Text _
Select _
Line = X.<SubjCode>.Value & " - " & _
X.<SubjectName>.Value & " - " & _
X.<DeptNum>.Value _
).ToArray
End Sub
Hope this helps.
Re: Stripping an XML and writing to a list box.
Quote:
Originally Posted by
amrita
Try this...I've only tested this for 1 co-id and multiple subjects.
Code:
Dim coll As New System.Collections.ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objReader As New System.IO.StreamReader("C:\co.xml") ' I'm assuming this is your file path
Dim intFirstPos As Integer
Dim intEndTagPos As Integer
Dim strLine As String
Dim strValue As String
While Not objReader.EndOfStream()
strLine = objReader.ReadLine
intFirstPos = InStr(strLine, "<CoordinatorID>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</CoordinatorID>", CompareMethod.Text)
strValue = Mid(strLine, intFirstPos + Len("<CoordinatorID>"), intEndTagPos - (intFirstPos + Len("<CoordinatorID>")))
If Not ComboBox1.Items.Contains(strValue) Then
ComboBox1.Items.Add(strValue)
End If
End If
End While
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
CreateCollection(ComboBox1.Text)
For Each Str As String In coll
ListBox1.Items.Add(Str)
Next
End Sub
Private Sub CreateCollection(ByVal strCoId As String)
Dim objReader As New System.IO.StreamReader("C:\co.xml")
Dim intFirstPos As Integer
Dim intEndTagPos As Integer
Dim strLine As String
Dim strCoIDValue As String
Dim strSubCodeValue As String
Dim strSubNameValue As String
Dim strDeptNumValue As String
While Not objReader.EndOfStream()
strLine = objReader.ReadLine
If strCoId = strCoIDValue Then
coll.Add(strSubCodeValue & " - " & strSubNameValue & " - " & strDeptNumValue)
strCoIDValue = ""
End If
intFirstPos = InStr(strLine, "<SubjCode>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</SubjCode>", CompareMethod.Text)
strSubCodeValue = Mid(strLine, intFirstPos + Len("<SubjCode>"), intEndTagPos - (intFirstPos + Len("<SubjCode>")))
End If
intFirstPos = InStr(strLine, "<SubjectName>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</SubjectName>", CompareMethod.Text)
strSubNameValue = Mid(strLine, intFirstPos + Len("<SubjectName>"), intEndTagPos - (intFirstPos + Len("<SubjectName>")))
End If
intFirstPos = InStr(strLine, "<DeptNum>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</DeptNum>", CompareMethod.Text)
strDeptNumValue = Mid(strLine, intFirstPos + Len("<DeptNum>"), intEndTagPos - (intFirstPos + Len("<DeptNum>")))
End If
intFirstPos = InStr(strLine, "<CoordinatorID>", CompareMethod.Text)
If intFirstPos > 0 Then
intEndTagPos = InStr(strLine, "</CoordinatorID>", CompareMethod.Text)
strCoIDValue = Mid(strLine, intFirstPos + Len("<CoordinatorID>"), intEndTagPos - (intFirstPos + Len("<CoordinatorID>")))
End If
End While
End Sub
Thank you both very much for your help, I ended up implementing this solution as it is more along the lines of what I've learnt. Just one question amrita, each time a new coordinator is selected it adds to the the end of the list, I would like it to clear the previous values and replace with the new ones. I am sure it is a very simple one line code that I need but can't for the life of me get it right! Thanks so much once again.
Re: Stripping an XML and writing to a list box.
Figured it out. Thank you guys so much for the help again. Life savers.
Re: Stripping an XML and writing to a list box.
Thanks..Please mark the post as resolved.