简体   繁体   中英

How to fetch value from string being returned from responsestream

I have the following code

response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
    Stream responseStream = response.GetResponseStream();
    string responseStr = new StreamReader(responseStream).ReadToEnd();
}

And the responseStr has the value <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cb8fbc96-05c6-4b9f-a64d-91a2d357c398</string>

I would like to fetch this value alone cb8fbc96-05c6-4b9f-a64d-91a2d357c398.

Any help much appreciated...Thank you in advance.

Seems on your client-side, it send a header to specific xml format as result.

There are several ways to do. If you could control your client-side, just set:

Content-Type=text/plain

If you can't control your client-side, another fairly easy way is to use reg-expression to get rid of those XML tags.

String patternXMLTag = @"<[^>]*>";
Regex XMLpattern = new Regex(PatternXMLTag, RegexOption.Compiled);
string result = Regex.Replace(val, patternXMLTag, string.Empty, RegexOptions.IgnoreCase);
//result is what you want
return result;

Of course, Microsoft provide XmlDocument and XmlElementand class helps you grab data out from a beauty formatted document. You could use them to easily grab data you want.

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