簡體   English   中英

正則表達式以匹配XML字符串在C#中具有開始和結束標簽

[英]Regular Expression To Match XML String is having start and end tags in C#

我想檢查一個xml字符串,它在C#中是否具有指定的開始和結束標簽。 xml字符串如下。

string xml=@"<iq from='someone' to='someone' id='1234' type='result'><query xmlns='jabber:iq:hello' type='hello'><question questionid='17' postedon='1376148890' editedon='1376150411' categoryid='1034' categoryname='Computer' postedby='mohan' viewstatus='0' availability='offline'>Hello</question></query></iq>";

<iq from='someone' to='someone' id='1234' type='result'>
<query xmlns='jabber:iq:hello' type='hello'>
<question questionid='17' postedon='1376148890' editedon='1376150411' categoryid='1034' categoryname='Computer' postedby='mohan' viewstatus='0' availability='offline'>Hello</question>
</query>
</iq>

我一直在使用以下正則表達式格式進行檢查,但對我沒有幫助

var regexForIq = new Regex(@"<iq(.*?)</iq>", RegexOptions.IgnoreCase);
var iqMatch = regexForIq.Match(_responseData);

提前致謝。

為什么不使用真正的xml解析器? http://msdn.microsoft.com/en-us/library/bb387061.aspx

var iq = XElement.Parse(xml).DescendantsAndSelf("iq").FirstOrDefault();
if (iq != null)
{
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM