2014-09-26 54 views
0

我需要獲取值狀態。從XML片段:正則表達式從XML元素中獲取內容

string xml = "<value z:Id=\"8\" z:Type=\"System.String\" z:Assembly=\"0\">Status.</value>"; 

Regex regexFieldValue = new Regex("z:Assembly=\"0\">(?<fieldValue>[^<|\\.|.]+)</value>"); 

Match match = regexFieldValue.Match(xml); 
if (match.Success) 
{ 
    Group group = match.Groups["fieldValue"]; 
    return group.Value; 
} 

坦克

+0

你的正則表達式。將' 「Z:裝配= \」 0 \ 「>(? [^ <>] +)」' – 2014-09-26 16:30:30

+1

通常的建議是 「使用解析器」,因爲XML ISN不是一種常規的語言。看到http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Brian 2014-09-26 16:31:52

+0

是的,但XML解析器是如此無聊,易於使用。寫一些難以理解的正則表達式要冷得多。 – 2014-09-26 16:53:50

回答

0

[^<|\\.|.]+不會做你的預期。所以在你的正則表達式中改爲[^<>]+

z:Assembly=\"0\">(?<fieldValue>[^<>]+)</value> 

DEMO