简体   繁体   中英

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

I want to check a xml string, wether that has the start and end specified tag in C#. The xml string is bellow.

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>

I have been using the following regex format to check but it doesn't help me

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

Thanks in advance.

Why don't you use a real xml parser? http://msdn.microsoft.com/en-us/library/bb387061.aspx

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

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