需要帮助才能正确编写XML LINQ查询

时间:2015-05-17 14:56:48

标签: c# .net xml vb.net linq

XML代码段

  <GameList>
  <Game>
    <GameDefinitiongameID>{1b2e4ff7-ddd3-4fe3-9fdf-6b2ee240d18c}</GameDefinitiongameID>
    <WMID>{01bc5c01-923d-4efb-bba6-49e93b2694ab}</WMID>
    <VersionNumberversionNumber>1.0.0.0</VersionNumberversionNumber>
    <Title>JamesBond007:Nightfire</Title>
    <GameExecutablepath>\easupport\go_ez.exe</GameExecutablepath>
    <GameExecutablepath>\easupport\jamesbond007nightfire_code.exe</GameExecutablepath>
    <GameExecutablepath>\easupport\jamesbond007nightfire_ereg.exe</GameExecutablepath>
    <GameExecutablepath>\easupport\jamesbond007nightfire_ez.exe</GameExecutablepath>
    <GameExecutablepath>jamesbond007nightfire_uninst.exe</GameExecutablepath>
    <GameExecutablepath>unwise.exe</GameExecutablepath>
    <RatingratingID>{CEC5DB5A-B4C9-4809-96C6-39CE715E4790}</RatingratingID>
    <ratingSystemID>{36798944-B235-48ac-BF21-E25671F597EE}</ratingSystemID>
    <DescriptordescriptorID>{F110F831-9412-40c9-860A-B489407ED374}</DescriptordescriptorID>
    <RatingratingID>{18CD34B7-7AA3-42b9-A303-5A729B2FF228}</RatingratingID>
    <ratingSystemID>{768BD93D-63BE-46A9-8994-0B53C4B5248F}</ratingSystemID>
    <DescriptordescriptorID>{B54162A2-F67F-46dc-9ED5-F6067520EC94}</DescriptordescriptorID>
    <DescriptordescriptorID>{BE562A5F-2A80-4c28-9752-74C696E2ABAF}</DescriptordescriptorID>
  </Game> 

等等。

我使用以下内容查询此文件

Dim members = From m In GameXml.Element("GameList").Elements("Game")
        Where m.Element("Title").Value.ToLower.Contains(Name) = True
                Select m.Descendants

&#34;名称&#34;是包含搜索字符串的变量。

现在,如何从&#34;成员&#34;获取所有信息(作为字符串数组或任何其他可解析格式)。变量

我是XML和LINQ的新手。

我对C#和VB.Net都很满意。

2 个答案:

答案 0 :(得分:2)

如果您不知道它具有哪些确切的属性,您可以这样做:

dynamic members = from m In GameXml.Element("GameList").Elements("Game")
        where m.Element("Title").Value.ToLower.Contains(Name) = True
                select m;

现在您可以使用:members.ratingSystemID

但是如果您知道属性,则可以创建类:

public class Game{
public string Title{set; get; }
public string Name {get; set;}
.
.
.
}

然后:

List<Game> members = from m In GameXml.Element("GameList").Elements("Game")
            where m.Element("Title").Value.ToLower.Contains(Name) = True
                    select new {Title = m.Title, Name = m.Name, ...};

因此您可以像Members[0].Title

一样使用它

答案 1 :(得分:1)

假设您想获得Game节点,那么您就这么近了:

Dim members = From m In GameXml.Element("GameList").Elements("Game")
     Where m.Element("Title").Value.ToLower.Contains(Name) = True
     Select m.Descendants("Game")

或(C#):

var members = GameXml.Descendants("Game")
      .Where(g=>g.Element("Title").Value.ToLower().Contains("Whatever"));