简体   繁体   English

在正确编写XML LINQ查询时需要帮助

[英]Need help in properly writing an XML LINQ query

XML Snippet 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> 

and so on. 等等。

I used the following to query this file 我用以下查询这个文件

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

"Name" is the variable containing the search string. “名称”是包含搜索字符串的变量。

Now, how to I get all the info (as array of strings or any other parse-able format) from the "members" variable. 现在,如何从“成员”变量中获取所有信息(以字符串数组或任何其他可解析格式表示)。

I am new to XML and LINQ. 我是XML和LINQ的新手。

I am OK with both C# and VB.Net. 我对C#和VB.Net都满意。

If you don't know what exact properties it has you can do this: 如果您不知道其确切的属性,可以执行以下操作:

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

now you can use: members.ratingSystemID 现在您可以使用: members.ratingSystemID

But if you know the properties you can create a class like: 但是,如果您知道属性,则可以创建一个类,如下所示:

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

and then: 接着:

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, ...};

so you can use it like Members[0].Title 因此您可以像Members[0].Title一样使用它

Assuming you want to get Game node, you're so close: 假设您想要获得“ Game节点,您是如此接近:

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

or (C#): 或(C#):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM