Help with creating list of countries found in xnl file

Hi,
I’m using the code below to identify all countries included as part of LinkFinal  (i.e. LinkFinalBEL)and listind them in a label control.  How do I modify the code the search for all countries in LinkTotal.xml and create a list for all countries found in the <Country></Country> data  element in LinkTotal.xml
'Create path to file location
        Dim destinationpath As String = IO.Path.Combine(Application.StartupPath, "LinkFiles\")
        Dim dirInfo = New System.IO.DirectoryInfo(destinationpath)
        Dim fileinfos = dirInfo.GetFiles("LinkFinal*.xml")
        Dim strcodes = fileinfos.Select(Function(f) f.Name) _
        .Aggregate("", Function(current, nextVal) current & "," & nextVal.Substring(nextVal.IndexOf("."c) - 3, 3)) _
        .Substring(2)
        Label4.Text = strcodes


For example if LinkTotal.xml isas follows:

<MainTable>
<MainID>1</MainID>
<Country>BEL</Country>
</MainTable>
<MainTable>
<MainID>2</MainID>
<Country>CAN</Country>
</MainTable>
<MainTable>
<MainID>3</MainID>
<Country>DEU</Country>
</MainTable>


I would like to create a list with BEL,CAN,DEU.

There will be duplicate values but I only won’t to see the country once in the list.

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

You can use XDocument from System.Xml.Linq namespace to parse xml document or xml string.

You can load with the help of this code-
http://msdn.microsoft.com/en-us/library/bb343181.aspx

Check this-
http://msdn.microsoft.com/en-us/library/bb348975.aspx

Try this code-
Dim countries = From country In xml.Descendants("MainTable") _
               Select country.Element("country").Value

For Each country In countries
     Debug.WriteLine("Country: " & country)
Next

Open in new window

0
ArkCommented:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xml As String = "<Root>"
        xml += "<MainTable>"
        xml += "<MainID>1</MainID>"
        xml += "<Country>BEL</Country>"
        xml += "</MainTable>"
        xml += "<MainTable>"
        xml += "<MainID>2</MainID>"
        xml += "<Country>CAN</Country>"
        xml += "</MainTable>"
        xml += "<MainTable>"
        xml += "<MainID>3</MainID>"
        xml += "<Country>DEU</Country>"
        xml += "</MainTable>"
        xml += "<MainTable>"
        xml += "<MainID>4</MainID>"
        xml += "<Country>CAN</Country>"
        xml += "</MainTable>"
        xml += "</Root>"
        Dim doc As New Xml.XmlDocument
        doc.InnerXml = xml
        MsgBox(String.Join(",", (From p In doc.SelectNodes("//Root/MainTable/Country")
                                 Select p.InnerText).Distinct.ToArray))
    End Sub

Open in new window

0
vcharlesAuthor Commented:
Thank you for all your comments.

I tried the code below, but received error 'Descendants is not a member of Xml'


Dim countries = From country In xml.Descendants("MainTable") _
               Select country.Element("country").Value

For Each country In countries
     Debug.WriteLine("Country: " & country)
Next


How do I fix this error?

Victor
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

vcharlesAuthor Commented:
Is there a way to implement the code below, without hard coding the data elemnts? For example by just opening the xml file?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xml As String = "<Root>"
        xml += "<MainTable>"
        xml += "<MainID>1</MainID>"
        xml += "<Country>BEL</Country>"
        xml += "</MainTable>"
        xml += "<MainTable>"
        xml += "<MainID>2</MainID>"
        xml += "<Country>CAN</Country>"
        xml += "</MainTable>"
        xml += "<MainTable>"
        xml += "<MainID>3</MainID>"
        xml += "<Country>DEU</Country>"
        xml += "</MainTable>"
        xml += "<MainTable>"
        xml += "<MainID>4</MainID>"
        xml += "<Country>CAN</Country>"
        xml += "</MainTable>"
        xml += "</Root>"
        Dim doc As New Xml.XmlDocument
        doc.InnerXml = xml
        MsgBox(String.Join(",", (From p In doc.SelectNodes("//Root/MainTable/Country")
                                 Select p.InnerText).Distinct.ToArray))
    End Sub
0
ArkCommented:
Dim doc As New Xml.XmlDocument
doc.Load("fileName.xml")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.