I have an xml-like text, in which I would like to find the item that occurs in the first occurrence of a certain pattern:
typically:
...
<PropertyGroup><name>true</name></PropertyGroup><PropertyGroup>....
....
Could also be
...
<PropertyGroup>
<name>
true</name>
</PropertyGroup>
...
<PropertyGroup>
...
In the above, I need to extract the “name”.
My initial assumption was that all occurrences were to be in one line, and I wrote my code using string properties, but it is very difficult o take in consideration every possibility, and only RegEx can save me.
I just don’t know how to write it…
I Have started with something like this:
Regex regex = new Regex("(?<=<PropertyGroup>#)<+");
Match matches = regex.Matches(Text)[0];
MessageBox.Show(matches.ToString());
I think this finds the first item after a <PropertyGroup>, but I don’t know how to make it get the item within the angular brackets… (which may be after one or more newlines, and/or spaces).
I know that there are utilities for parsing xml, but I am looking for something simple to insert in a c# program
Can someone please help me ? Thank you very much.
Edit:
Actual file content (the one I am testing now, with no weird spaces):
<?xml version="1.0" ?><Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import xxx/>
</ImportGroup>
<PropertyGroup><myProp>true</myProp></PropertyGroup><PropertyGroup Label="UserMacros"/>
<PropertyGroup/>
<!--maybe other stuff -->
</Project>
Using LINQ to XML is really quite simple and is much more reliable than using regular expressions:
…
Now
propertiesshould contain all theXElementobjects for the immediate children of allPropertyGroupelements. You can that get both their names and values with:This way you do not have to worry about any kind of whitespace and it is a very maintainable and extensible solution to go on and retrieve other information from the XML.
Plus, it is really something generally worth reading up on and here is the place to start. This will not be the last time you need to handle an XML file, and you will be glad if you do not always have to figure out a regex to parse (which, let me repeat that, is not even generally possible).