I have a string that is basically an XML node, and I need to extract the values of the attributes. I am trying to use the following C# code to accomplish this:
string line = "<log description="Reset Controls - MFB - SkipSegment = True" start="09/13/2011 10:29:58" end="09/13/2011 10:29:58" timeMS="0" serviceCalls="0">"
string pattern = "\"[\\w ]*\"";
Regex r = new Regex(pattern);
foreach (Match m in Regex.Matches(line, pattern))
{
MessageBox.Show(m.Value.Substring(1, m.Value.Length - 2));
}
The problem is that this is only returning the last occurrence from the string (“0” in the above example), when each string contains 5 occurrences. How do I get every occurrence using C#?
Don’t try to parse XML with regular expressions. Use an XML API instead. It’s simply a really bad idea to try to hack together “just enough of an XML parser” – you’ll end up with incredibly fragile code.
Now your line isn’t actually a valid XML element at the moment – but if you add a
</log>it will be.That’s slightly hacky, but it’s better than trying to fake XML parsing yourself.