use*_*048 2 c# linq where-clause
我正在尝试匹配XML文件上的特定术语并保存结果.这是字符串上的XML文本:
<node1>
<node2>
<text>Goodbye</text>
</node2>
<node2>
<text>Welcome James</text>
</node2>
<node2>
<text>Welcome John</text>
</node2>
<node2>
<text>See you later!</text>
</node2>
</node1>
Run Code Online (Sandbox Code Playgroud)
我想使用linq来选择任何欢迎的文本.然而欢迎之后的名字(例如欢迎詹姆斯)可以改变.因此,我试图理解是否有一种简单的方法可以通过正则表达式选择具有任何欢迎名称的节点?
这是C#代码:
private static void Test(string stream)
{
XDocument doc = XDocument.Parse(stream); //stream contains the xml written above
var list = from hello in doc.Descendants("node2")
where attacker.Element("text").Value == "Welcome .*"
select attacker.Element("text").Value;
foreach (var x in attackList)
Console.WriteLine(x);
}
Run Code Online (Sandbox Code Playgroud)
对于像您这样简单的场景,不需要使用正则表达式.您可以使用String.StartsWith(String)方法确定字符串是否以指定的字符串开头,如下所示:
private static void Test(string stream)
{
XDocument doc = XDocument.Parse(stream);
var list = from hello in doc.Descendants("node2")
where attacker.Element("text").Value.StartsWith("Welcome")
select attacker.Element("text").Value;
foreach (var x in attackList)
{
Console.WriteLine(x);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
904 次 |
| 最近记录: |