获取<>之间的值动态数字不起作用

时间:2015-12-24 09:50:13

标签: c# regex

我有一个这样的字符串:

<sentence id='s0'>
 The nature of the proceeding 

1 The principal issue in this proceeding is whether the Victorian Arts Centre falls within the category of 'premises of State Government Departments and Instrumentalities', for the purposes of provisions in industrial awards relating to rates of payment for persons employed in cleaning those premises.</sentence>

<sentence id='s1'>In turn, this depends upon whether the Victorian Arts Centre Trust, a statutory corporation established by the Victorian Arts Centre Act 1979 (Vic) ('the VAC Act'), is properly described as a State Government department or instrumentality, for the purposes of the award provisions.</sentence>
;

我需要这个输出:

The nature of the proceeding 

     1 The principal issue in this proceeding is whether the Victorian Arts Centre falls within the category of 'premises of State Government Departments and Instrumentalities', for the purposes of provisions in industrial awards relating to rates of payment for persons employed in cleaning those premises.

In turn, this depends upon whether the Victorian Arts Centre Trust, a statutory corporation established by the Victorian Arts Centre Act 1979 (Vic) ('the VAC Act'), is properly described as a State Government department or instrumentality, for the purposes of the award provisions.

我应该在<sentence id='s0'></sentence> and <sentence id='s1'></sentence> .

之间提取字符串

我在这里Get the value between <> with dynamic number inside it提出了这个问题,解决方案是这样的:

var res = XElement.Parse(xml)
                  .Descendants("sentence").Where(e => e.Attribute("id").Value == "s0")
                  .FirstOrDefault().Value;

我的输入中有190个句子。 但是这段代码只是提取我​​的意思是第一个标签:

The nature of the proceeding 

    1 The principal issue in this proceeding is whether the Victorian Arts Centre falls within the category of 'premises of State Government Departments and Instrumentalities', for the purposes of provisions in industrial awards relating to rates of payment for persons employed in cleaning those premises.

如何提取所有标签值,我的输入中有190个句子

1 个答案:

答案 0 :(得分:3)

您可以这样做:

var res = XElement.Parse(xml)
    .Descendants("sentence")
    .Select(s => s.Value)
    .ToArray();

res将是一个字符串数组 - 每个sentence XML元素的内部内容。

例如,res[0]将是:

The nature of the proceeding 

1 The principal issue in this proceeding is whether the Victorian Arts Centre falls within the category of 'premises of State Government Departments and Instrumentalities', for the purposes of provisions in industrial awards relating to rates of payment for persons employed in cleaning those premises.

In turn, this depends upon whether the Victorian Arts Centre Trust, a statutory corporation established by the Victorian Arts Centre Act 1979 (Vic) ('the VAC Act'), is properly described as a State Government department or instrumentality, for the purposes of the award provisions.