I want to manipulate the following HTML structure using C#, and move the bgcolor attribute inside the style attribute as shown below, I want to achieve it using string manipulation (Or any other suitable method if applicable) is there a way possible:
Present Structure
<body>
<div bgcolor="#342516" style="color: red; font-size:10px;">ABCD</div>
<div bgcolor="#342516" style="color: red; font-size:10px;">EFGH</div>
<div bgcolor="#342516" style="color: red; font-size:10px;">HIJK</div>
<div bgcolor="#342516" style="color: red; font-size:10px;">LMNO</div>
</body>
Required Output
<body>
<div style="background-color:#342516; color: red; font-size:10px;">ABCD</div>
<div style="background-color:#342516; color: red; font-size:10px;">EFGH</div>
<div style="background-color:#342516; color: red; font-size:10px;">HIJK</div>
<div style="background-color:#342516; color: red; font-size:10px;">LMNO</div>
</body>
CodePudding user response:
I don't think you need to program to solve this, a text editor will suffice. As far as I know, both VS Code and NotePad support replacing text by folder.
Replace bgcolor="#342516" style=" with style="background-color:#342516;
CodePudding user response:
If You really need it in C# the most simple solution is string.replace(). for any other more complex text You need regular expression
var oldString = "<body>\r\n <div bgcolor=\"#342516\" style=\"color: red; font-size:10px;\">ABCD</div>\r\n <div bgcolor=\"#342516\" style=\"color: red; font-size:10px;\">EFGH</div>\r\n <div bgcolor=\"#342516\" style=\"color: red; font-size:10px;\">HIJK</div>\r\n <div bgcolor=\"#342516\" style=\"color: red; font-size:10px;\">LMNO</div>\r\n</body>";
var newString = oldString.Replace("bgcolor=\"#342516\" style=\"", "style=\"background-color:#342516; ");