C# 获取之间的值,其中包含动态数字

C# 获取之间的值,其中包含动态数字,c#,regex,string,match,C#,Regex,String,Match,我正在研究一种文本摘要方法,为了测试我的方法,我有一个名为Doc2007的基准测试,在这个基准测试中我有很多xml文件,我应该清除这个文件 例如,我有这样一个xml文件: <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

我正在研究一种文本摘要方法,为了测试我的方法,我有一个名为Doc2007的基准测试,在这个基准测试中我有很多xml文件,我应该清除这个文件

例如,我有这样一个xml文件:

<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.
Regex.Match("User name (sales)", @"\(([^)]*)\)").Groups[1].Value

使用Regex,但它不起作用。您能给我一个快速解决方案吗?

使用LINQ to XML应该更容易:

var res = XElement.Parse(xml)
                  .Descendants("sentence").Where(e => e.Attribute("id").Value == "s0")
                  .FirstOrDefault().Value;
或者,正如Yeldar所建议的,更干净的方法是:

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

使用LINQ到XML应该更容易:

var res = XElement.Parse(xml)
                  .Descendants("sentence").Where(e => e.Attribute("id").Value == "s0")
                  .FirstOrDefault().Value;
或者,正如Yeldar所建议的,更干净的方法是:

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

Parse仅在具有单个根节点的字符串中使用。您编写的实例有两个节点,但没有一个根节点。 您可以添加根节点,如下所示:

xml = "<root>" + xml + "</root>";

Parse仅在具有单个根节点的字符串中使用。您编写的实例有两个节点,但没有一个根节点。 您可以添加根节点,如下所示:

xml = "<root>" + xml + "</root>";

既然有一个格式良好的XML,为什么要用正则表达式解析它?使用XML解析器!我不熟悉xml解析器xml处理是自v1以来.NET的一个基本部分。您可以使用旧的XMLDocument、新的XDocument、XPath查询等,甚至可以在数据表中加载XML文档。Web服务ASMX或WCF。如果不了解可用的XML类,就不能在.NET中编程。另一方面,正则表达式不适合于XML解析,除了一些非常简单的情况。您有一个格式良好的XML,为什么要尝试使用正则表达式解析它?使用XML解析器!我不熟悉xml解析器xml处理是自v1以来.NET的一个基本部分。您可以使用旧的XMLDocument、新的XDocument、XPath查询等,甚至可以在数据表中加载XML文档。Web服务ASMX或WCF。如果不了解可用的XML类,就不能在.NET中编程。另一方面,正则表达式不适合XML解析,除了一些非常简单的情况。第二句应该包括在内。你的答案只是提取第一句而不是。其中e=>e.Attributeid.Value==s0.FirstOrDefault它可以是。FirstOrDefault=>e.Attributeid.Value==s0:我得到了这个错误:有多个根元素。第5行,位置2。亲爱的friend@EhsanAkbar很可能,有多个根元素:一个格式良好的XML文件应该只包含一个根元素。应该包含第二句话您的答案只需提取第一句而不是。其中e=>e.Attributeid.Value==s0.FirstOrDefault它可以是。FirstOrDefault=>e.Attributeid.Value==s0:我得到了这个错误:有多个根元素元素。第5行,位置2。亲爱的friend@EhsanAkbar很可能存在多个根元素:格式良好的XML文件应该只包含一个根元素。