Question

I am doing a CLR procedure of sending mail, I have composed the html into my string , After binding some dynamic values I have been able to send mail.

But Now the issue is , I am getting a string which containing the HTML, So I want to find the first <table> and then change its width. it.[Due to large width the template is disturbing]. This is the body of the email.

 string StrHtml =" <table cellspacing='1' cellpadding='10' border='0'
style='width:880px'></table>"

I want to change the style='width:880px' to style='width:550px'

I am doing this code only in class library. what is the best way to do this?

My Code is :

string ImgPath = string.Empty;
ImgPath = Convert.ToString(ds.Tables[0].Rows[i]["GroupMessage"]);
string pattern = string.Empty;
pattern = System.Text.RegularExpressions.Regex.(ImgPath, "(<table.*?>.*</table>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Groups[1].Value;  

MailMessage message = new MailMessage();
message.AlternateViews.Add(htmlMail);
message.Body = ImgPath ; //
message.IsBodyHtml = true;
//Other mail sending code here.....
Was it helpful?

Solution 4

I am getting the html string in below format and I want to get only the attribute value of style="width: 880px" , which is always changing.[This is only work if the width is mention in style tag. And that is the my case.]

ImgPath ==>

 <table border="0" cellpadding="10" cellspacing="1" style="width: 880px">
    // some other code goes here
    </html>

Answer ==> I used regular expression in my class, [want to make DLL file from that class]

string StrStyTag = string.Empty;
StrStyTag = System.Text.RegularExpressions.Regex.Match(ImgPath, "<table.+?style=(.+?)>", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Groups[1].Value;

I am getting the value as ==> "width: 880px", So after doing some string manipulations I make the 880px to fix 550px.

Thanks for all those who help me to resolved it.

OTHER TIPS

you could use the HtmlAgilityPack, and something like this:

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(StrHtml);

var tableNode = ( from node in htmlDoc.DocumentNode.Descendants()
                 where node.Name == "table"
                 select node ).FirstOrDefault();

if ( tableNode != null ) {
    tableNode.Attributes["style"].Value = 
        tableNode.Attributes["style"].Value.Replace("880px", "550px");
}

You can do this with XDocument:

    using (var reader = new StringReader(StrHtml))
    {
       var doc = XDocument.Load(reader);
       var table = doc.Descendants("table").FirstOrDefault();
       if (table != null)
       {
           var style = table.Attribute("Style");
           if (style != null)
               style.Value = "width:550px";
       }
    }

What's wrong with just this?

StrHtml = StrHtml.Replace("880px","550px");

Or was that just an example? Sorry it isn't clear.

EDIT:

With Regex (using System.Text.RegularExpressions;):

StrHtml = Regex.Replace(StrHtml, "width:[\d]*px", "width:550px");

Please note that the "width:(numbers)px" text must exist (and width must be in the same lower case as the Regex) or you will get an exception. But then, just surround it with a try{StrHtml = Regex.Replace(StrHtml, "width:[\d]*px", "width:550px");}catch{} and you'll be fine

FINAL FINAL EDIT:

You want to get the style width for only the table tag, I gather:

try{
StrHtml = Regex.Replace(StrHtml, @"(<table[\s\S]*)(width:[\d]*px)(.*?>)", @"$1width:550px$3");
}
catch{}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top