简体   繁体   中英

Regular Expression for xml element

I have an xml in the form of a string. Let's say that the element name is TestDate and the date is the form YYYY-MM-DD . So in other words <TestDate>1950-03-31</TestDate> .

How do I find this.

Lets say I have string xml;

xml contains the element 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}

if you can use Perl-RegEx-Syntax, you can use \\d instead the [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. Load xml string in XmlDocument and extract all similar nodes. Below code will return all TestDate Nodes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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