Results 1 to 12 of 12

Thread: [RESOLVED] Stripping an XML and writing to a list box.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Resolved [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

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    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:
    1. Console.WriteLine("Querying tree loaded with XDocument.Load");
    2.                 XDocument loaded = XDocument.Load(sourceFile);
    3.                 var Coordinators = from Coordinator in loaded.Descendants("subject")
    4.                                                 select new
    5.                 {
    6.                     Position = Coordinator.Element("SubjCode").Value,
    7.                     CoordinatorId = Coordinator.Element("SubjectName").Value
    8.                 };
    9.                 foreach (var c in Coordinators)
    10.                 {
    11.                     Console.WriteLine("{0},{1}", c.CoordinatorId, c.Position);
    12.                 }
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Stripping an XML and writing to a list box.

    Quote Originally Posted by kevininstructor View Post
    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
    Last edited by Rmckenna; Aug 12th, 2010 at 08:45 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Stripping an XML and writing to a list box.

    Quote Originally Posted by abhijit View Post
    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:
    1. Console.WriteLine("Querying tree loaded with XDocument.Load");
    2.                 XDocument loaded = XDocument.Load(sourceFile);
    3.                 var Coordinators = from Coordinator in loaded.Descendants("subject")
    4.                                                 select new
    5.                 {
    6.                     Position = Coordinator.Element("SubjCode").Value,
    7.                     CoordinatorId = Coordinator.Element("SubjectName").Value
    8.                 };
    9.                 foreach (var c in Coordinators)
    10.                 {
    11.                     Console.WriteLine("{0},{1}", c.CoordinatorId, c.Position);
    12.                 }
    Sorry, but I really don't understand this

  7. #7
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Stripping an XML and writing to a list box.

    Read this and this.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  8. #8
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile 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
    thanks
    amrita

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Stripping an XML and writing to a list box.

    Quote Originally Posted by amrita View Post
    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.

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    10

    Re: Stripping an XML and writing to a list box.

    Figured it out. Thank you guys so much for the help again. Life savers.

  12. #12
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Stripping an XML and writing to a list box.

    Thanks..Please mark the post as resolved.
    thanks
    amrita

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
  •  



Click Here to Expand Forum to Full Width