I need to manipulate a XML string.
The string is this one :
<div class="addthis_toolbox addthis_default_style ">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
I thought I would convert it into a XmlDocument, but XmlDocument.LoadXml() throws an error about the “:” character ; it’s because of the fb:like:layout attribute.
What I need to do, is add an addthis:url attribute to the first element with a addthis_toolbox or addthis_button class.
I’m pretty confident that I can find the element with the correct class, but I’m not really confident that I can add a “composite” attribute like that… especially since I can’t even load the thing to a XmlDocument.
Did I miss something ? Is there a better/simpler way ?
Thanks
Provided XML isn’t well-formed, so you can’t manipulate it using XML parser.
You can perform pre-processing of this text, so it becomes well-formed XML, then manipulate it as XML using XML engine.
EDIT:
Read: RegEx match open tags except XHTML self-contained tags
But may be in your case usage of regex is most appropriate, if you structure of input HTML is regular, e.g.:
You can use this regex
to find
div class="addthis_toolbox addthis_default_style ", then replace this string, i.e.:Result: