一样使用
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>easupportgo_ez.exe</GameExecutablepath>
<GameExecutablepath>easupportjamesbond007nightfire_code.exe</GameExecutablepath>
<GameExecutablepath>easupportjamesbond007nightfire_ereg.exe</GameExecutablepath>
<GameExecutablepath>easupportjamesbond007nightfire_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
"Name"是包含搜索字符串的变量。
现在,我如何从"成员"变量中获得所有信息(作为字符串数组或任何其他可解析格式)。
我是XML和LINQ的新手。
我对c#和VB.Net都很好。如果你不知道它的确切属性,你可以这样做:
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
假设你想获得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"));