How Parse XML Using Regular Expressions And C#

written by $post.User.ProperName on Thursday, February 28 2008

I regularly find myself needing a quick way to parse a smallish block of XML and extract the contents of a number of known elements. Now at this point I must confess that I am not good with regular expressions, but I am trying to learn so here is one that makes my life easier:
(?<=Identifier>)[\S\s]*?(?=\<\/Identifier)
What this block does is mach on everything in the element, while NOT matching the element itself. So if you take the following block of XML:
 
Jason R. Shaver
Male
30-39
Quite handsome really...
 
 
And pass it into the following ProcessParameters method:
private void ProcessParameters(String xml)
{
_Identifier = ExtractContentFromXml(xml, "Identifier", String.Empty);
_Notes = ExtractContentFromXml(xml, "Notes", String.Empty);
_Gender = ExtractContentFromXml(xml, "Gender", String.Empty);
_GivenAge = ExtractContentFromXml(xml, "GivenAge", String.Empty);
}
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;
}
You get the outputs, and this SHOULD be easier to read (for the programmer, not the program) then using the XML classes. I have not checked the performance of this method over the System.Xml namespace classes, but in a program that looped through 100 xml files and extracting the above it took less than a second.

Similar Posts

  1. An Easy .NET CF Settings Class
  2. Improving Your Internet Sales Performance
  3. Regular Expression Stuff

Comments

  • Turkin on on 12.05.2008 at 3:05 PM

    Turkin avatar

    String RegExpression = String.Format(@"(?<={0}>)[\S\s]*?(?=\<\/{0})", element);

  • jasonrshaver on on 3.13.2009 at 9:31 AM

    jasonrshaver avatar

    I just noticed that my "Block of text" got the xml tags stripped out =(

Post a comment

Options:

Size

Colors