Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<processresponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><extensiondata /><errorcode>0</errorcode><errormessage /><message>successful.</message></processresponse>



i have to extract only..

HTML
<errorcode>0</errorcode><errormessage /><message>successful.</message>
Posted
Updated 10-Sep-14 19:54pm
v2

The whole string is also XML. So, better parse the whole thing and then use the data from the element you are interested in. .NET offers different ways of parsing XML; below is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
You can use below regular expression to extract desired string.

<errorcode>.*<\/message>

C#
string str = @"<processresponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><extensiondata /><errorcode>0</errorcode><errormessage /><message>successful.</message></processresponse>";
Regex reg = new Regex(@"<errorcode>.*<\/message>");
Match tmp = reg.Match(str);
Console.WriteLine(tmp.Value);
 
Share this answer
 
v2
Comments
Naveen Singh 11-Sep-14 2:04am    
i am fetching this string from sql database...then how can i use this expression.Thanks Syed Asif
Naveen Singh 12-Sep-14 2:42am    
@Syed Asif tmp.vale returns null ....!
_Asif_ 12-Sep-14 2:52am    
static void Main(string[] args)
{
string str = @"<processresponse xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><extensiondata><errorcode>0<errormessage><message>successful.</processresponse>";
Regex reg = new Regex(@"<errorcode>.*<\/message>");
Match tmp = reg.Match(str);
Console.WriteLine(tmp.Value);
}

tmp.Value =
"<errorcode>0</errorcode><errormessage></errormessage><message>successful.</message>"</errorcode>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900