Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 正则表达式-选择但不选择_.net_Regex_C# 4.0 - Fatal编程技术网

.net 正则表达式-选择但不选择

.net 正则表达式-选择但不选择,.net,regex,c#-4.0,.net,Regex,C# 4.0,我不熟悉正则表达式,希望为我的.net应用程序创建两个正则表达式 我有一个存储xml的输入变量。下面是xml <Row> ...... </Row> <Row {optional}> .... </Row> <Row {optional} header="true" {optional}> </Row> ...... .... 我想要2个正则表达式: 1.Regex,用于选择header=“true”的行 2.Re

我不熟悉正则表达式,希望为我的.net应用程序创建两个正则表达式

我有一个存储xml的输入变量。下面是xml

<Row>
......
</Row>
<Row {optional}>
.... 
</Row>
<Row {optional} header="true" {optional}>
</Row>

......
.... 
我想要2个正则表达式: 1.Regex,用于选择header=“true”的行 2.Regex,用于选择没有header=“true”的行


<>正则表达式只需要考虑打开标记。例如:

正则表达式不是在.NET中处理xml的最佳选择 如果您使用的是.NET3.5或更高版本,请查看 有关较旧版本的运行时,请参阅

使用LINQ,您的代码如下:

    var document = XDocument.Load("path to your file"); // or XDocument.Parse if you have content in a string
    var elementsWithHeaders = document.Descendants("Row").Where(x => 
         x.Attribute("header")!=null && x.Attribute("header").Value == "true");
这段代码不是最优的,但是为了提高效率,我需要了解更多关于xml结构的假设

如果您使用的是C#6,那么还可以使用来简化上面的谓词

    var elementsWithHeaders = document.Descendants("Row").Where(x => 
         x.Attribute("header")?.Value == "true");