Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

In a .cs file I have a string variable like
string body = " <p>
            Maximize the quality, efficiency and value of your applications with enterprise
            application modernization, testing and management software from Micro Focus.</p>
        <p>
            Products A - Z: <a title="Products A - Z (A)">
href='/products/productsa-z/index.aspx'>
                </a> J K L <a title="Products A - Z (M)" href="/products/productsa-z/productsa-zM.aspx">
                                        M</a> <a title="Products A - Z (N)" href="/products/productsa-z/productsa-zN.aspx">
                </a> U <a title="Products A - Z (V)" href="/products/productsa-z/productsa-zV.aspx">
                                                                V</a> <a title="Products A - Z (W)" href="/products/productsa-z/productsa-zW.aspx">                                                                    W</a> <a title="Products A - Z (X)" href="/products/productsa-z/productsa-zx.aspx">
                                                                        X</a>
            Y Z</p>
        <div style="border-removed #cecece 1px; border-removed #cecece 1px solid; border-removed #cecece 1px solid; padding-removed 10px">
            <p>
                <a href="/products/isight/index.aspx">Application Portfolio Management and Analysis</a><br />
                <img style='width: 11px; height: 11px; vertical-align: middle' title='arrow_badge_icon.gif'
                    alt='arrow_badge_icon.gif' src='/assets/arrow_badge_icon.gif' width='11' height='11' /><a>
                        href='/products/isight/index.aspx'>i.Sight</a></p>
            <p>
                Tools applications.</p>
        </div>        
        <div style="border-removed #cecece 1px solid;padding-removed 10px">
            <p>
                <a href="/products/micro-focus-developer/index.aspx">COBOL and Software Developer Tools</a><br />
                <img style='width: 11px; height: 11px; vertical-align: middle' 
title='arrow_badge_icon.gif'  alt='arrow_badge_icon.gif' src='/assets/arrow_badge_icon.gif' width='11' height='11' /><a href="/products/micro-focus-developer/index.aspx">Micro Focus Developer</a></p>
            <p>
                Industry applications.</p>
        </div>        <br />
        <br />
        <p>
        </p>";  


The above string is an extract and there are lots of a tags in it. I want to manipulate this string. I want to modify the anchor tags in it , i have to read the anchor tags title and add a " onclick=" event to each anchor tag(Each anchor tag will have a different title and the title will be included in the onclick event as a dynamic variable) Can anyone help me on this. What approach should i opt for?
Posted
Updated 7-Jul-11 19:59pm
v2
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 2:05am    
Format it properly, please -- impossible to read.
--SA
Sergey Alexandrovich Kryukov 8-Jul-11 2:07am    
What kind of help do you need?
--SA

 
Share this answer
 
Comments
BobJanova 8-Jul-11 6:32am    
Good answer.
[no name] 8-Jul-11 8:30am    
Thanks
Prasanta_Prince 11-Jul-11 1:21am    
Good one.
One of the possible way to address this problem:

You string looks like a well-formed XML which is not too big. You can pass it as XML. You can do it using the class System.Xml.XmlTextReader to get a DOM structure or the class System.Xml.Linq.XDocument to get a document tree structure. In both cases, you will get a structured document with access to your HTML nodes. Modify the nodes as you need and serialize the document back to XML text.

See:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^],
http://msdn.microsoft.com/en-us/library/bb387063.aspx[^],
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx[^].

[EDIT: in case of not well-formed XML]

If HTML can be not a well-formed XML, you can use appropriate HTML parser. Try this: http://www.majestic12.co.uk/projects/html_parser.php[^].

—SA
 
Share this answer
 
v2
Comments
angelvarun 8-Jul-11 2:26am    
Hi SAKryukov,

Thanks for replying. My string is a big string and it can increase in size and it's not exactly well formed xml. But you can say its bit HTML type. So i was just looking for help regarding manipulating the anchor tags in it and then getting the modified string variable.
BobJanova 8-Jul-11 6:31am    
Are you generating the HTML? If it is possible to generate well formed XML, SAK's approach will be more robust than the regex based one Irfan mentions.
Sergey Alexandrovich Kryukov 11-Jul-11 1:17am    
I was expecting that it might not be well-formed XML. In this case, try HTML parser, please see update in my solution, after [EDIT].
--SA
angelvarun 8-Jul-11 7:39am    
Hi Bob,
Unfortunately the html is added by client.
Sergey Alexandrovich Kryukov 12-Jul-11 15:33pm    
I see. Did you see my update? It helps to address the problem in the case of not well-formed XML.
--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