dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] VB.net: Query XDocument Variable using Linq

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    4

    Resolved [RESOLVED] VB.net: Query XDocument Variable using Linq

    Greetings, my first post on VBForums here.
    Could someone help me out with querying an Xdocument variable with linq, I know there is tons of tutos in the net but they all seem to load xml files then reading hard coded variables, so this is what I've got so far in my function :
    Code:
    Public Function GetCountryByCode(CountryCode As String)
                   Dim COUNTRYLIST = <?xml version="1.0" encoding="utf-8"?>
                  <COUNTRIES>
                  <COUNTRY><COUNTRYID>OCA1</COUNTRYID><COUNTRYNAME>USA</COUNTRYNAME></COUNTRY>
                  <COUNTRY><COUNTRYID>NAM1</COUNTRYID><COUNTRYNAME>AUSTRALIA</COUNTRYNAME></COUNTRY>
                 </COUNTRIES>
    
    Dim xCountry As IEnumerable(Of String) = From cnt In COUNTRYLIST.Descendants("COUNTRY")
    Where cnt.Element("COUNTRYCODE").Equals(CountryCode) Select cnt.Element("COUNTRYNAME").Value
    
            Return xCountry.LastOrDefault
    
    End Function
    For now my function returns nothing.
    Thanks.

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    162

    Re: VB.net: Query XDocument Variable using Linq

    Code:
        Public Function GetCountryByCode(CountryCode As String) As String
            Dim COUNTRYLIST As System.Xml.Linq.XDocument = <?xml version="1.0" encoding="utf-8"?>
                                                           <COUNTRIES>
                                                               <COUNTRY><COUNTRYID>OCA1</COUNTRYID><COUNTRYNAME>USA</COUNTRYNAME></COUNTRY>
                                                               <COUNTRY><COUNTRYID>NAM1</COUNTRYID><COUNTRYNAME>AUSTRALIA</COUNTRYNAME></COUNTRY>
                                                               <COUNTRY><COUNTRYID>NL</COUNTRYID><COUNTRYNAME>Hollandia!</COUNTRYNAME></COUNTRY>
                                                           </COUNTRIES>
    
            Return COUNTRYLIST.Descendants("COUNTRY").Where(Function(x) x.Element("COUNTRYID").Value.Equals(CountryCode)).Select(Function(y) y.Element("COUNTRYNAME").Value).FirstOrDefault()
    
        End Function
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    4

    Re: VB.net: Query XDocument Variable using Linq

    Thank you Goggy for your help.
    I gave it a try with your code but returns a System.NullReferenceException: System.Xml.Linq.XContainer.Element(...) returned Nothing.

    Just to make things clear, My function intends to lookup "COUNTRYNAME" by "COUNTRYID".

    Thanks again.

  4. #4
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    162

    Re: VB.net: Query XDocument Variable using Linq

    I have tested the code here and it works...

    when i call the function like so

    Code:
      GetCountryByCode("NL")
    I get Hollandia! as a result

    ok, the code isnt fail prove.. you could do with a try catch around it... but it should work
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    4

    Re: VB.net: Query XDocument Variable using Linq

    Oops! My bad.
    I was trying the function in the immediate window without prefixing it with a question mark, it actually worked like a charm.
    Thank you very much dud!! Best regards

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,219

    Re: [RESOLVED] VB.net: Query XDocument Variable using Linq

    $0.02 using xelement

    Code:
    Public Class Form1
    
        Private COUNTRYLIST As XElement
        Public Function GetCountryByCode(CountryCode As String) As String
            Dim path As String = "path to xml file"
            'load from file
            'COUNTRYLIST = XElement.Load(path)
    
            'for test use literal
            COUNTRYLIST = <COUNTRIES>
                              <COUNTRY><COUNTRYID>OCA1</COUNTRYID><COUNTRYNAME>USA</COUNTRYNAME></COUNTRY>
                              <COUNTRY><COUNTRYID>NAM1</COUNTRYID><COUNTRYNAME>AUSTRALIA</COUNTRYNAME></COUNTRY>
                              <COUNTRY><COUNTRYID>NAM1</COUNTRYID><COUNTRYNAME>AUSTRALIA2</COUNTRYNAME></COUNTRY>
                              <COUNTRY><COUNTRYID>NL</COUNTRYID><COUNTRYNAME>Hollandia!</COUNTRYNAME></COUNTRY>
                          </COUNTRIES>
    
            Dim xCountry As IEnumerable(Of XElement)
            'first match
            xCountry = From el In COUNTRYLIST.<COUNTRY>
                       Where el.<COUNTRYID>.Value = CountryCode
                       Select el Take 1
    
            Dim rv As String = ""
            If xCountry.Count > 0 Then
                rv = xCountry.<COUNTRYNAME>.Value
            End If
            Return rv
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String = GetCountryByCode("no match")
            Debug.WriteLine(s)
            s = GetCountryByCode("NAM1")
            Debug.WriteLine(s)
        End Sub
    End Class
    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
  •  



Featured


Click Here to Expand Forum to Full Width