Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
i have a problem when matching the following xml:
Regex im using :
<contextualmenu[\s\s]*?><focus[\s\s]*?<Selection[\s\s]*?>/b>


Result is that im capturing both (ContextualMenu) tags while i need to catch each one separately...
Thanks for your help !


XML
<ContextualMenu EventID="23">
            <Focus CaretPosition="0">
                <Selection  zone="Label"/>
            </Focus>
        </ContextualMenu>
        <ActionPerformed EventID="24" >
            <ViewSelection>
                <Selection  zone="Label" />
            </ViewSelection>
        </ActionPerformed>


<ContextualMenu  token="sample"   EventID="10">
            <Focus CaretPosition="0">
                <Selection  zone="Label"/>
            </Focus>
        </ContextualMenu>
        <ActionPerformed EventID="24" >
            <ViewSelection>
                <Selection  zone="Label" />
            </ViewSelection>
        </ActionPerformed>
Posted
Updated 29-Apr-11 5:02am
v2
Comments
Sandeep Mewara 29-Apr-11 11:12am    
Can you elaborate more on the issue you are facing than what you are trying to do?

Save yourself the pain and use XPath expressions[^] to dissect your XML.
Regular expression are really no good for parsing nested structures.

Best Regards,

-MRB
 
Share this answer
 
v2
Comments
Kim Togo 29-Apr-11 11:28am    
Good answer. My 5. Regexp will fail on complex XML and HTML files.
Sergey Alexandrovich Kryukov 29-Apr-11 22:21pm    
Agree with you (less sure about XPath for this particular task, probably you though about it), so my 5.
My approach would be XML schema, please see my answer.
--SA
Manfred Rudolf Bihy 2-May-11 6:43am    
What I took from OP's question was that he wanted to extract XML tags. Nevertheless validation your XML is always a good thing if you want to be sure that the data you're retrieving conforms to waht is expected.
Sergey Alexandrovich Kryukov 2-May-11 7:16am    
Sure.
--SA
You can use LINQ to XML. It is very nice to work with.
LINQ to XML Overview[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-11 22:20pm    
This should give a right idea, my 5.
My approach would be using the XML schema. Also see my general conclusion :-)
--SA
In addition to what Kim and Manfred proposed, I'll add:

You need to create an XML schema and validate XML against the schema, please read this: http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschemavalidator.aspx[^].

General conclusion: application of Regex to XML is totally pointless. You don't want to do it!

—SA
 
Share this answer
 
Comments
Manfred Rudolf Bihy 2-May-11 6:25am    
My 5!
Sergey Alexandrovich Kryukov 2-May-11 7:05am    
Thank you, Manfred.
--SA

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