Click here to Skip to main content
15,846,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have converted code from c# to
C# code

static void Main(string[] args)
           string[] strarr = GetStringArray("Locations.xml");

           foreach (string str in strarr)

       public static string[] GetStringArray(string url)
            XDocument doc = XDocument.Load(url);

           var locations = from l in doc.Descendants("Location")
                           select (string)l.Attribute("Name");

           return locations.ToArray();

vb code:
Private Shared Sub Main(args As String())
       Dim strarr As String() = GetStringArray("Locations.xml")

       For Each str As String In strarr
   End Sub

   Public Shared Function GetStringArray(url As String) As String()
       Dim doc As XDocument = XDocument.Load(url)

   Dim locations = From l In doc.Descendants("Location")DirectCast(l.Attribute("Name"), String)

       Return locations.ToArray()
   End Function

but the below code is not working in especially

Dim locations = From l In doc.Descendants("Location")DirectCast(l.Attribute("Name"), String)


Could you please help that how to cast the above value into string

What I have tried:

I have tried with the below code
Dim locations = From l In doc.Descendants("Location")DirectCast(l.Attribute("Name"), String)
Updated 26-Dec-16 1:39am

1 solution

There are several ways of going about this: you can use the query expression syntax,
Dim locations = From l In doc.Descendants("Locations")
                Select CStr(l.Attribute("Name"))
or you could avoid the query expression syntax and just use the standard dot notation syntax,
Dim locations = doc.Descendants("Locations").Select(Function(l) l.Attribute("Name").Value)
or you could use axis properties,
Dim locations = doc...<Locations>.Select(Function(l) l.@Name)
Share this answer
DGKumar 26-Dec-16 8:29am    
Thanks for respond its working..Could you please tell me that how to get multiple columns in same logic?Like
Dim locations = doc.Descendants("Locations").Select(Function(l) l.Attribute("Name").Value)
Dim locations2 = doc.Descendants("Locations").Select(Function(l) l.Attribute("city").Value)
Both i need to store in one dim variable.
DGKumar 26-Dec-16 8:37am    
I have tried with the below code but not working
Dim svcResultFields = XMLData.Elements("Locations").Select(Function(f) new{ Name = f.Attribute("Name").Value,Type = f.Attribute("City").Value}).ToArray()
Meshack Musundi 26-Dec-16 12:54pm    
You can get a collection of anonymous types by doing,
Dim results = doc...<Locations>.Select(Function(l) New With {.Name = l.@Name, .Type = l.@City})

Dim results = doc.Descendants("Locations").Select(Function(l) New With {.Name = l.Attribute("Name").Value, .Type = l.Attribute("City").Value})
Maciej Los 27-Dec-16 3:02am    
DGKumar 27-Dec-16 3:08am    
Hi Now i am able to get both values.
If i want to get values from that result i am getting the below error not able to get values in foreach in.
Error: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]' to type 'System.IConvertible'

I have tried :
Dim results() As String = provisioningXMLData.Descendants("Locations").Select(Function(l) New With {.Video = l.Attribute("Name ").Name , .Type = l.Attribute("Type ").Value})
For Each deniedStatus As String In results
If deniedStatus.Name IsNot Nothing Then
isHSIAdataDenied = True
isHSIAdataDenied = False
End If

Exit For

I am not able get proper foreach in for that collection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900