Linq查询XML数据 [英] Linq query for XML data

查看:59
本文介绍了Linq查询XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将代码从c#转换为vb.net

C#代码



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

foreach string str in strarr)
{
Console.WriteLine(str);
}
}

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

var locations = 来自 l in doc.Descendants( Location
选择 string )l。属性( 名称);

return locations.ToArray();
}







vb代码:

 私人 共享  Sub  Main(args  As   String ())
Dim strarr As String ()= GetStringArray( Locations.xml

对于 每个 str 作为 字符串 strarr
Console.WriteLine(str)
下一步
结束 Sub

公共 共享 函数 GetStringArray(url As 字符串作为 字符串()
Dim doc As XDocument = XDocument.Load(url)

Dim locations =从l doc.Descendants( 位置 DirectCast (l.Attribute( 名称),字符串

返回 locations.ToArray()
结束 功能





但以下代码在vb.net es中无效pecially

  DirectCast  





< pre lang =C#> Dim locations = From l in doc.Descendants(& quot; Location& quot;)DirectCast(l.Attribute(& quot; Name& quot;),字符串





  DirectCast  



你能帮忙解决一下如何将上述值转化为字符串



我尝试了什么:



我尝试使用以下代码

 Dim locations = From l in doc.Descendants(& quot; Location& quot;)DirectCast(l.Attribute(& quot; Name& quot;), String 

解决方案

有几种方法可以解决这个问题:你可以使用查询表达式语法,

  Dim  locations = from l  doc.Descendants( 位置
选择 CStr (l.Attribute( 名称))

或者您可以避免查询表达式语法,只需使用标准点表示法语法,

  Dim  locations = doc.Descendants( < span class =code-string>位置)。选择功能(l )l.Attribute(  Name)。Value)

或者你可以使用轴属性,

  Dim  locations = doc ...< Locations>。选择功能(l)l。 @name)


I have converted code from c# to vb.net
C# code

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

           foreach (string str in strarr)
           {
               Console.WriteLine(str);
           }
       }

       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
           Console.WriteLine(str)
       Next
   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 vb.net especially

DirectCast



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



DirectCast


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(&quot;Location&quot;)DirectCast(l.Attribute(&quot;Name&quot;), String)

解决方案

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)


这篇关于Linq查询XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆