Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linq to XML Using Visual Basic

Posted on 2012-04-06
4
Medium Priority
?
515 Views
Last Modified: 2012-04-06
How do I use Linq to get a list of countries that start with the letter 'B'?

Here's the data:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" minOccurs="0" />
                <xs:element name="CountryName" minOccurs="0">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:maxLength value="255" />
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <CountryName>Afghanistan</CountryName>
  </Table1>
  <Table1>
    <ID>2</ID>
    <CountryName>Albania</CountryName>
  </Table1>
  <Table1>
    <ID>3</ID>
    <CountryName>Algeria</CountryName>
  </Table1>
  <Table1>
    <ID>4</ID>
    <CountryName>Andorra</CountryName>
  </Table1>
  <Table1>
    <ID>5</ID>
    <CountryName>Angola</CountryName>
  </Table1>
  <Table1>
    <ID>6</ID>
    <CountryName>Antigua and Barbuda</CountryName>
  </Table1>
  <Table1>
    <ID>7</ID>
    <CountryName>Argentina</CountryName>
  </Table1>
  <Table1>
    <ID>8</ID>
    <CountryName>Armenia</CountryName>
  </Table1>
  <Table1>
    <ID>9</ID>
    <CountryName>Australia</CountryName>
  </Table1>
  <Table1>
    <ID>10</ID>
    <CountryName>Austria</CountryName>
  </Table1>
  <Table1>
    <ID>11</ID>
    <CountryName>Azerbaijan</CountryName>
  </Table1>
  <Table1>
    <ID>12</ID>
    <CountryName>Bahrain</CountryName>
  </Table1>
  <Table1>
    <ID>13</ID>
    <CountryName>Bangladesh</CountryName>
  </Table1>
  <Table1>
    <ID>14</ID>
    <CountryName>Barbados</CountryName>
  </Table1>
  <Table1>
    <ID>15</ID>
    <CountryName>Belarus</CountryName>
  </Table1>
  <Table1>
    <ID>16</ID>
    <CountryName>Belgium</CountryName>
  </Table1>
  <Table1>
    <ID>17</ID>
    <CountryName>Belize</CountryName>
  </Table1>
  <Table1>
    <ID>18</ID>
    <CountryName>Benin</CountryName>
  </Table1>
  <Table1>
    <ID>19</ID>
    <CountryName>Bhutan</CountryName>
  </Table1>
  <Table1>
    <ID>20</ID>
    <CountryName>Bolivia</CountryName>
  </Table1>
  <Table1>
    <ID>21</ID>
    <CountryName>Bosnia and Herzegovina</CountryName>
  </Table1>
  <Table1>
    <ID>22</ID>
    <CountryName>Botswana</CountryName>
  </Table1>
  <Table1>
    <ID>23</ID>
    <CountryName>Brazil</CountryName>
  </Table1>
  <Table1>
    <ID>24</ID>
    <CountryName>Brunei</CountryName>
  </Table1>
  <Table1>
    <ID>25</ID>
    <CountryName>Bulgaria</CountryName>
  </Table1>
  <Table1>
    <ID>26</ID>
    <CountryName>Burkina Faso</CountryName>
  </Table1>
  <Table1>
    <ID>27</ID>
    <CountryName>Burundi</CountryName>
  </Table1>
  <Table1>
    <ID>28</ID>
    <CountryName>Cambodia</CountryName>
  </Table1>
  <Table1>
    <ID>29</ID>
    <CountryName>Cameroon</CountryName>
  </Table1>
  <Table1>
    <ID>30</ID>
    <CountryName>Canada</CountryName>
  </Table1>
 </NewDataSet>
0
Comment
Question by:Ray Turner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37817642
Here is one example:

Module Module1

    Sub Main()
        Dim query = From country In System.Xml.Linq.XDocument.Load("input.xml").Descendants("CountryName") _
                    Where country.Value.StartsWith("B") _
                    Select country.Value

        For Each countryName In query
            Console.WriteLine(countryName)
        Next

        Console.ReadKey()
    End Sub

End Module

Open in new window

0
 

Author Comment

by:Ray Turner
ID: 37817764
I got following error.  How do I fix?

Expression of type 'System.Collections.Generic.IEnumerable(Of System.Xml.Linq..XElement)' is not queryable
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 2000 total points
ID: 37817974
Sorry about that. Add Imports System.Linq to the top of your file.
0
 

Author Closing Comment

by:Ray Turner
ID: 37818145
Thanks!!!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question