2014-02-07 11 views
1

我需要的是一個字符串返回代碼中的錯誤,我可以在php做到這一點,但它需要被轉換爲C#和任何一個把這段代碼轉換爲C#從返回的StreamReader查找錯誤代碼

<?xml version="1.0" encoding="UTF-8"?> 
<nma> 
    <error code="402" resettimer="TIMELEFT"></error> 
</nma> 

preg_match("/<error code=\"(.*?)\".*>(.*?)<\/error>/i", $return, $out); 


// return $out[1] = 200; 

我都沒有成功

foreach (Match m in Regex.Matches(response, "<error code=\"(.*?)\".*>(.*?)</error>")){ 
     Console.WriteLine("'{0}' found at index {1}.", 
          m.Value, m.Index); 
+1

爲什麼不使用xml解析器?正則表達式不是用於解析xml –

回答

1

我認爲你正在尋找Match.Groups屬性嘗試這樣做:

string response = @"<?xml version=""1.0"" encoding=""UTF-8""?> 
        <nma> 
         <error code=""402"" resettimer=""TIMELEFT"">test</error> 
        </nma>"; 

foreach (Match m in Regex.Matches(response, "<error code=\"(.*?)\".*>(.*?)</error>")) 
{ 
    Console.WriteLine(m.Groups[1]); // 402 
    Console.WriteLine(m.Groups[2]); // test 
} 
+0

,它比我想要的更好 – user3265026