Aug 8, 2012

How Parse XML Using Regular Expressions in Asp.net with C#


Hello Friends,

I got good solution for XML element value find from XML string.
Need to just call this function with parameters XML string, XML element name and default value if value not find into XML string.

 private String ExtractContentFromXml(String xml, String element, String defaultValue)
{
         String RegExpression = String.Format(@"(?<={0}>)[\S\s]*?(?=\<\/{0})", element);
         Match ThisMatch = Regex.Match(xml, RegExpression);
        if (ThisMatch.Success)
                  return ThisMatch.Value;
        else
                 return defaultValue;
}

I hope you will enjoy this solution...

1 comment: