Results 1 to 7 of 7

Thread: Read XML with a loop

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    121

    Read XML with a loop

    I have a simple XML file, and I'd like to loop through it, rather than using the IF ElseIf iteration to read it, is this possible. My code works but if I add items to the XML file I will have to modify the code to read it correctly, my code is below, please offer help if you can, Thanks in advance...


    Code:
       Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim ConfigStrings As String = vbNullString
    
            ''check if file Config.xml exists
            If (File.Exists(AppPath() & "\Data\Config.xml")) Then
    
                'create a new xmltextreader object
                'this is the object that we will loop and will be used to read the xml file
                Dim document As XmlReader = New XmlTextReader(AppPath() & "\Data\Config.xml")
                'loop through the xml file
    
                While (document.Read())
                    Dim type = document.NodeType
                    'if node type was element
                    If (type = XmlNodeType.Element) Then
    
                        'if the loop found a <FirstWarning> tag
                        If (document.Name = "FirstWarning") Then
                            ConfigStrings = document.ReadInnerXml.ToString()
    
                            'if the loop found a <SecondWarning tag
                        ElseIf (document.Name = "SecondWarning") Then
                            ConfigStrings = ConfigStrings & "," & document.ReadInnerXml.ToString()
    
                            'if the loop found a <ThirdWarning tag
                        ElseIf (document.Name = "ThirdWarning") Then
                            ConfigStrings = ConfigStrings & "," & document.ReadInnerXml.ToString()
    
                            'if the loop found a <FourthWarning tag
                        ElseIf (document.Name = "FourthWarning") Then
                            ConfigStrings = ConfigStrings & "," & document.ReadInnerXml.ToString()
                        End If
                    End If
                End While
            End If
    
            SearchName = Split(ConfigStrings, ",")
    
            Me.Text = "Visual Studio 2008 File Cleaner ©    " & My.Application.Info.Version.ToString
        End Sub

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Read XML with a loop

    Can you post the file or part of it? Using some sample data
    Code:
            Dim xe As XElement
            'to load from a file
            '  xe = XElement.Load("Your Path Here")
    
             ' for testing
            xe = <root>
                     <FirstWarning id="1">Lorem</FirstWarning>
                     <SecondWarning id="1">ipsum</SecondWarning>
                     <ThirdWarning id="1">dolor</ThirdWarning>
                     <FirstWarning id="2">consectetur</FirstWarning>
                     <SecondWarning id="2">adipisicing</SecondWarning>
                     <ThirdWarning id="2">incididunt</ThirdWarning>
                 </root>
    you could get each warning and process it using LINQ. Here is firstwarning

    Code:
            Dim ie1 As IEnumerable(Of XElement) = xe...<FirstWarning>
            Dim s As String = String.Join(", ", ie1.Select(Function(w) w.Value).ToArray, 0, ie1.Count)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    121

    Re: Read XML with a loop

    Yes thanks for the response, here is the file very simple...

    <?xml version="1.0" encoding="utf-8"?>
    <!--Remove Upgrade Warnings-->
    <Data>

    <!--Enter Items to search for, and remove-->

    <FirstWarning>UPGRADE_WARNING</FirstWarning>
    <SecondWarning>UPGRADE_NOTE</SecondWarning>
    <ThirdWarning>UPGRADE_TODO</ThirdWarning>
    <FourthWarning>UPGRADE_ISSUE</FourthWarning>

    </Data>

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Read XML with a loop

    Quote Originally Posted by supercrewed View Post
    Yes thanks for the response, here is the file very simple...

    <?xml version="1.0" encoding="utf-8"?>
    <!--Remove Upgrade Warnings-->
    <Data>

    <!--Enter Items to search for, and remove-->

    <FirstWarning>UPGRADE_WARNING</FirstWarning>
    <SecondWarning>UPGRADE_NOTE</SecondWarning>
    <ThirdWarning>UPGRADE_TODO</ThirdWarning>
    <FourthWarning>UPGRADE_ISSUE</FourthWarning>

    </Data>
    Gives this a try. Note that I added two optional items that 1) clears the node values, 2) removes the warnings entirely.

    Code:
            xe = XElement.Load("Your Path Here")
            'get all of the warnings
            Dim ie1 As IEnumerable(Of XElement) = xe...<FirstWarning>
            ie1 = ie1.Concat(xe...<SecondWarning>)
            ie1 = ie1.Concat(xe...<ThirdWarning>)
            ie1 = ie1.Concat(xe...<FourthWarning>)
    
            'make a string
            Dim s As String = String.Join(", ", ie1.Select(Function(w) w.Value).ToArray, 0, ie1.Count)
    
            '---------------------  OPTIONAL ----------------------------
            'this clears all of the elements values
            ie1.ToList.ForEach(Sub(w)
                                   w.Value = ""
                               End Sub)
    
            'this removes the elements
            ie1.ToList.Remove()
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    121

    Re: Read XML with a loop

    Nice Thanks, I played with it a bit, and noticed I could add a fifth, through seventh warning and run the code, and it's still good, AWESOME...
    I have another program I made be able to incorporate this into, whereas I read in defaults for machine code,

    Thanks so much, still playing with it...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    121

    Re: Read XML with a loop

    Is it possible to modify this code, along with the XML file and use a for next loop, to iterate the through the nodes of the XML file... Something like this, I tried it but so far it hasn't worked.

    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim xe As XElement
            Dim appPath As String = My.Application.Info.DirectoryPath
    
            Dim n As String = "Warning_"
            Dim i As Integer
    
            xe = XElement.Load(appPath & "\test.xml")
            'get all of the warnings
    
            For i = 1 To xe.IsEmpty
                Dim ie1 As IEnumerable(Of XElement) = xe...<n & i >
    
            Next
    
    
            'make a string
            Dim s As String = String.Join(vbCrLf, ie1.Select(Function(w) w.Value).ToArray, 0, ie1.Count)
            TextBox1.Text = (s)
            '---------------------  OPTIONAL ----------------------------
            'this clears all of the elements values
            ie1.ToList.ForEach(Sub(w)
                                   w.Value = ""
                               End Sub)
    
            'this removes the elements
            ie1.ToList.Remove()
    
        End Sub
    End Class

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Read XML with a loop

    When you originally posted I should have asked, are you the author of the XML? If you are creating the file then what you might do is use an attribute to identify the warnings. The attribute would have 1 for first, 2 for second, three for third, etc. Here is a way to create your data

    Code:
            Dim xe As XElement
            'to load from a file
            '  xe = XElement.Load("Your Path Here")
    
            ' for testing
            xe = <Data>
                 </Data>
    
            Dim warningProto As XElement = <warning id=""></warning> 'warnings have this basic format
    
            'create some warnings
            For i As Integer = 1 To 10
                Dim w As XElement = New XElement(warningProto) 'note the use of New
                w.@id = i.ToString
                w.Value = "e.g. " & i.ToString 'used for example purposes
                xe.Add(w)
            Next
    The following examples assume the code above.

    Then to create a string of all warnings

    Code:
            'make string
            Dim ie1 As IEnumerable(Of XElement) = xe...<warning>
            Dim s As String = String.Join(Environment.NewLine, ie1.Select(Function(w) w.Value).ToArray, 0, ie1.Count)
    If you want a particular warning you can do this where I am looking for 7's

    Code:
            Dim ie1 As IEnumerable(Of XElement)
            Dim idnum As Integer
            ie1 = From el In xe...<warning>
                  Where Integer.TryParse(el.@id, idnum) AndAlso idnum = 7
                  Select el
    
            Dim s As String = String.Join(Environment.NewLine, ie1.Select(Function(w) w.Value).ToArray, 0, ie1.Count)
    Here is a range of warnings (2 - 5),

    Code:
            Dim ie1 As IEnumerable(Of XElement)
            Dim idnum As Integer
            ie1 = From el In xe...<warning>
                  Where Integer.TryParse(el.@id, idnum) AndAlso
                    idnum > 1 AndAlso idnum < 6
                  Select el
    
            Dim s As String = String.Join(Environment.NewLine, ie1.Select(Function(w) w.Value).ToArray, 0, ie1.Count)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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