Changing html with Regex in C# - c#

I've got source string like
<p>blablabla</p>
<p><img style="float: left;" src="../Content/attachments/455dd178-db28-4856-85e8-c65c8e6b04df_312540909.jpg" alt="455dd178-db28-4856-85e8-c65c8e6b04df_312540909.jpg" />blablabla</p>
<p><img style="float: right;" src="../Content/attachments/dec0f850-2921-4bf7-87b8-d2410e04a841_image001.gif" alt="dec0f850-2921-4bf7-87b8-d2410e04a841_image001.gif" width="100" /></p>
For each img element I need to remove alt attribute, and replace src elements with srcFileName.Substring(37).
Can't figure out the regex needed. Please help.

Had to use Html Agility Pack for this

Related

Aspx get value from confirmed HTML

I would like to get value from label with id=lblValue. On page confirm I see confirmed HTML as:
<span class="treeColorOrderBefPrepare">~
<div style='display:inline-block;'>
<img onclick='calculateDate.add(this);' style='cursor:pointer;' class="imageInBox" src="/_assets/images/icons/small/add.png"/>
<img onclick='calculateDate.subtract(this);' style='margin-left:3px;cursor:pointer;' class="imageInBox" src="/_assets/images/icons/small/subtract.png"/>
<label id='lblValue' style='position:relative;top:1px;width: 13px; height: 13px;margin-left:3px;margin-right:3px;'>0</label>
</div>
<img class="imageInBox" src="/_assets/images/icons/small/icon-small-merchandise.png"/>POPER ČRNI MLETI (0,0 KG) (fina. HKJ_TK, predp. Cent.predp: 04.04.2017 - 0,0 KG)</span>
<input type="text" name="DK_m_1_3_120000204_140000240-120003917-120000204-" value="">
All I need is to get value from lblValue. Is there a way to get this value? Values will range from -10 to 0. In the example above, value=0. I am doing this on the server side, not on the client side.
<script type="javascript">
document.getElementById("lblValue").innerHTML
</script>
Add runat="server" attribute like this:
<label id='lblValue' runat="server" style='position:relative;
top:1px;width: 13px; height: 13px; margin-left:3px;margin-right:3px;'>0</label>
and you will be able to use lblValue as a variable.
For more information, what runat attribute is doing, please see this: Why does ASP.NET webforms need the Runat="Server" attribute?

Regular expression : parse html to certain html

We having a HTML content like
<em></ em >
<font style="text-align:justify;">aaaaaaaaaaa</font>
<img src="abc.jpg"/>
<iframe src="somelink.com">
</iframe>
<br>
<br/>
We want to change all HTML Tags to <p></p>
but do not change the <img/> and <br/> tag, some <br/> tags may display <br>
so, the following is our expected result:
<p></p>
<p>aaaaaaaaaaa</p>
<img src="abc.jpg"/>
<p>
</p>
<br>
<br/>
My regular expression like (in C#):
String result = Regex.Replace(content, #"<[^/b>]*>", "<p>");
result = Regex.Replace(result, #"</[^>]*>", "</p>");
but it can't skip the certain tags,
please help me, thanks !
You can use this:
<(?<close>/?)((?!img|br).)*?>
and replace with:
<${close}p>
CODE SAMPLE

how to get wrap text using div tag?

I need to get the "wrap text" in div tag, here is what I'm using:
<div style="width:650px;height:200px;overflow-y:scroll;display: inline-block; writing-mode:lr-tb; word-wrap:break-word;white-space:normal;">
<asp:Label ID="bodyLabel" runat="server" Text=""></asp:Label>
</div>
Can any one please suggest me how to implement this.
Thanks in advance.
what about loosing the label tag , and place your text in the div directly :
<div ID="bodyLabel" runat="server" style="width:650px;height:200px;overflow-y:scroll;display: inline-block; writing-mode:lr-tb; word-wrap:break-word;white-space:normal;">
</div>
and the text will wrap by default in the div.
dont you think its easier ?
Have you tried CSS? See this link.

how to render HTML entities to normal character into ASP literal control?

how there,
I've these codes in my DATABASE in other words it's HTML. I tried these stuff:
<div runat="server" id="div1" visible="false">
<asp:Literal ID="literal1" runat="server" Text="" />
</div>
I tried in C# code behind:
div1.InnerText = contents;
div1.InnerHtml = contents
literal1.Text = contents;
But is still doesn't render well. I displays the original values in stead of a table and cells and columns. colours etc. etc....
What am I missing?
All these HTML's are in DABASE.Column e.g. column "Contents"
e.g.
"& lt;p class=& quot;MsoNormal" style= "color: #339966;">&lt"
;&quot ;&gt ;&lt ;strong &gt ;&l
ot; &gt ;&amp ;nbsp; &lt ;/span >&lt ;/p >
Can someone please advice? what I'm I misssing?
I've put (spaces between & and gt above code otherwise it was not showing in stackoverflow.) The HTML sysntaxs are correct because it's created by an HTMLEDITOR.
use
literal1.Text = this.HtmlDecode(contents);
Try response.Write(Contents)
And take a look at HtmlDecode
Are you saying that ;&gt ;&lt etc is displaying on the browser, or only when you view the source?

Replace newlines with <p> paragraph and with <br /> tags

So I know how to replace newlines in my C# code. But replacing a newline for a <br /> tag isn't always very correct.
So I was wondering what kind of strategy do others use? The correct way I guess would be to use <p> tags and <br /> tags.
Here are some examples of the results I would like to get.
If there is no newline I want the text to wrapped in a <p> tags.
This text contains no newlines
<p>This text contains no newlines</p>
If the text contains a newline I want it to be replaced by a <br /> tag and be wrapped in <p> tags.
This text contains
1 newline
<p>This text contains<br /> 1 newline.</p>
If there are 'double newlines' I want that block to be wrapped in <p> tags.
This is a text with 'double newlines' at the end.
This is a text with no newline at the end.
<p>This a text with 'double newlines at the end.</p>
<p>This is a text with no newline at the end.</p>
I could write more examples/combination but I guess it's somewhat clear what I mean.
Thanks in advance.
Here's a way you could do it using only simple string replacements:
string result = "<p>" + text
.Replace(Environment.NewLine + Environment.NewLine, "</p><p>")
.Replace(Environment.NewLine, "<br />")
.Replace("</p><p>", "</p>" + Environment.NewLine + "<p>") + "</p>";
Note that your text must be HTML-escaped first otherwise you could be at risk of cross-site scripting attacks. (Note: even using <pre> tags still has a cross-site scripting risk).
You could just leave it alone and use CSS to render the breaks correctly. Here is a complicated example that is a kind of "pretty" replacement for the <pre> but you are using a <p> instead:
<p style="padding: 1em; line-height: 1.1em; font-family: monospace; white-space: pre; overflow: auto; background-color: rgb(240,255,240); border: thin solid rgb(255,220,255);">
Text goes here.
</p>

Categories