简体   繁体   English

使用Linq从XML文档的value字段返回子字符串到XML

[英]Returning a substring from the value field of an xml doc using Linq to XML

The XML field is as such : XML字段是这样的:

<Element> 0 (0)</Element>

As far as I know this should do what I wish, you can ignore the irrelevant part (e.Descendants) as this is correct (and I have verified this) in the greater scheme of my query. 据我所知,这应该做我希望做的事情,您可以忽略不相关的部分(例如,后代),因为这在我的更大查询方案中是正确的(并且我已经验证了这一点)。 :

Regex regularExpression = new Regex(@"(\d+) \((\d+)\)", RegexOptions.IgnorePatternWhitespace);

And the Linq to XML in question : 还有Linq to XML的问题:

variableName =  e.Descendants().Where(i => regularExpression.IsMatch(i.Value.ToString())).Select(j => regularExpression.Match(j.Value.ToString()).Groups[1].Value.ToString()).ToList()

However when I attempt to print this value, it is not printing anything. 但是,当我尝试打印此值时,它没有打印任何内容。

What have I done incorrectly here ? 我在这里做错了什么?

您的正则表达式应使用'\\s'而不是' '

Regex regEx2 = new Regex(@"(\d+)\s\((\d+)\)", RegexOptions.IgnorePatternWhitespace);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM