简体   繁体   English

xml元素的正则表达式

[英]Regular Expression for xml element

I have an xml in the form of a string. 我有一个字符串形式的xml。 Let's say that the element name is TestDate and the date is the form YYYY-MM-DD . 假设元素名称为TestDate,日期格式YYYY-MM-DD So in other words <TestDate>1950-03-31</TestDate> . 所以换句话说<TestDate>1950-03-31</TestDate>

How do I find this. 我怎么找到这个。

Lets say I have string xml; 假设我有字符串xml;

xml contains the element TestDate. xml包含元素TestDate。 I want to get that using a regular expression. 我想使用正则表达式得到它。

string regularExpression = @"";
Regex regex = new Regex(regularExpression , RegexOptions.Compiled);

What should be going in for regular expression. 正则表达式应该包含什么内容。

This should match your Date: [0-9]{4}-[0-9]{2}-[0-9]{2} 这应与您的日期匹配: [0-9]{4}-[0-9]{2}-[0-9]{2}

if you can use Perl-RegEx-Syntax, you can use \\d instead the [0-9] 如果可以使用Perl-RegEx语法,则可以使用\\d代替[0-9]

If you want search data in that element only than itshould be 如果您只想在该元素中搜索数据

<TestDate>[0-9]{4}-[0-9]{2}-[0-9]{2}</TestData>

Another aproach is to use XPath. 另一个方法是使用XPath。 Load xml string in XmlDocument and extract all similar nodes. 在XmlDocument中加载xml字符串并提取所有类似的节点。 Below code will return all TestDate Nodes. 下面的代码将返回所有TestDate节点。

XmlNode[] nodes = doc.SelectNodes("//TestDate");

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

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