Results 1 to 2 of 2

Thread: [RESOLVED] Help using this code for multiple XDocument nodes

  1. #1
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 11
    Posts
    144

    Resolved [RESOLVED] Help using this code for multiple XDocument nodes

    Ok, so I've got this code and it works great with the fact that it does exactly what I want, except for the fact that it'll only work with one node then move where I need it to work with one node then run the check again for any nodes after that one that have the same value in an attribute. The idea behind this is:

    the ListItem has an attribute with a program location and what time to start/end the specified program.

    Any idea what direction I need to go for this to work through the full document?

    vb Code:
    1. Dim iTime As String = hours & ":" & minutes & ap
    2.  
    3.         Try
    4.             Dim xml As XDocument = XDocument.Load(xmlDatabase)
    5.  
    6.             Dim e = From element
    7.                 In xml.Root.Elements("ListItem")
    8.                 Where (element.Attribute("StartTime").Value = iTime)
    9.                 Select element
    10.  
    11.             Dim ex As XAttribute = e.Attributes.ElementAt(2)
    12.             Dim sTime As String = ex.Value
    13.             If iTime.Equals(sTime) Then
    14.                 Dim ax As XAttribute = e.Attributes.ElementAt(1)
    15.                 Process.Start(ax.Value)
    16.             End If
    17.         Catch
    18.         End Try
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

  2. #2
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 11
    Posts
    144

    Re: Help using this code for multiple XDocument nodes

    Ah, the power of google. So I looked for about an hour before I posted this and kept looking while I waited on an answer. Here is what I found for myself:

    vb Code:
    1. Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
    2. Dim employees As IEnumerable(Of XElement) = xelement.Elements()
    3. ' Read the entire XML
    4. For Each employee In employees
    5.       Console.WriteLine(employee)
    6. Next employee

    I managed to adapt it to:

    vb Code:
    1. Dim iTime As String = hours & ":" & minutes & ap
    2.  
    3.         Try
    4.             Dim xel As XElement = XElement.Load(xmlDatabase)
    5.             Dim ListItem As IEnumerable(Of XElement) = xel.Elements()
    6.             For Each item In ListItem
    7.                 If item.ToString.StartsWith("<ListItem") Then
    8.  
    9.                     If iTime.Equals(item.Attribute("StartTime").Value) Then
    10.                         Dim ax As XAttribute = item.Attributes.ElementAt(1)
    11.                         Process.Start(ax.Value)
    12.                     End If
    13.                 End If
    14.             Next
    15.         Catch ex As Exception
    16.  
    17.         End Try

    And that seems to be doing exactly what I need it to. Thanks for the help google
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •