Format HTML with C# - c#

I want to format html code with C#.
From this:
<!DOCTYPE html><html><head><title>Hello World!</title></head><body><h1>Hi, HTML!</h1></body></html>
I want this:
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hi, HTML!</h1>
</body>
</html>
I tried:
string result = System.Xml.Linq.XElement.Parse(source).ToString();
And this works, but if I try this with Google website code, it throws some exceptions because
doctypeisn't written correctly.
How can I format HTML code without HTML syntax error check?
Can anyone help me? Thanks in advance!

Related

Korean utterances are not well mapping into GeneratePdf from html content

I am generating pdf from this HTML, and for achieving this I am using https://www.nrecosite.com/pdf_generator_net.aspx
This is the HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>
<p>Remarks</p>
<p>한국어 발언을 표시하는 테스트입니다</p>
</div>
</body>
</html>
and this the code for generating the pdf:
var pdfBytes = new
NReco.PdfGenerator.HtmlToPdfConverter().GeneratePdf(html);
and the result is this:
how can I generate the pdf with the correct Korean utterances?
Thanks

How can i retrieve the value of this element and assert it after a change is done?

I am working on automating an area of a web page (not able to provide the webpage as the contents are confidential, although will try to give as much insight as possible).
This element has on it an html code preview that will change after some selections are done. Here is the page html of the element:
<div _ngcontent-hje-c241="" class="field" style="position: relative;">
<pre _ngcontent-hje-c241="" class="code-pre">
"
<!doctype html>
<html>
<head>
<meta charset='utf'>
<title></title>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />
<style type='text/css'>body,html, #video {height:100%;margin:0;overflow:hidden;background-color:#000;}</style>
<script type='text/javascript'>
window.onload = function(event) {
var config = {containerID: 'video', Player: true, wmode: 'direct'};
myplayer = new Test.embed('WmAl', config);
}
</script>
</head>
<body>
<div id='video'></div>
</body>
</html>"
(I have edited and removed any confidentials parts of the string for the html, the html itself was not changed.)
I would need to get the value of the element I found through class="code-pre".
Here is what I have tried:
IWebElement htmlTest = driver.FindElement(By.ClassName("code-pre"));
var defaultHtmlTestValue = htmlTest.GetAttribute("value");
Assert.IsFalse(htmlTest.Equals(defaultHtmlTestValue), "The html has not changed after the Http selection");
The assert passes, altho, i would like to see what is the value that is being taken, as i feel like is not taking the html example i am trying to get.
I have also used Debug.Writeline(htmlTest) to see if it worked, but i got "Internal error in the expression evaluator". This is also an issue i will be trying to fix.
I am quite new to automation and stack overflow. Please let me know if there is a way i can improve this post.
I haven't worked with the html previews but looks like the whole html code is just the inner text of an element with class code-pre.
try doing :
String htmlTest = driver.FindElement(By.ClassName("code-pre")).getTex();
System.out.println(htmlTest);
Assert.assertTrue(htmlTest.Equals(defaultHtmlTestValue), "The html has not changed after the Http selection");
You can easily convert above java code into your language.

Run C# UrlEncode String in HTML

I need to URL encode an email address. If I do this (without URL encode), it works:
<!DOCTYPE html>
<head>
<title>Test</title>
</head>
<body>
<a href="https://www.example.com/reset-password?e=%7bEMAILADDRESS%7d">
</body>
</html>
The email address is printed raw, not URL-encoded.
What I'd really like to do is the following, but HttpUtility.UrlEncode() doesn't execute:
<!DOCTYPE html>
<head>
<title>Test</title>
</head>
<body>
<a href="https://www.example.com/reset-password?e=HttpUtility.UrlEncode("%7bEMAILADDRESS%7d")">
</body>
</html>
Any suggestions on how to execute HttpUtility.UrlEncode() for usage in the above manner? Thanks for any help.
You need to use <%= %>
like:
<a href="https://portal.nchinc.com/reset-password?e=
<%=HttpUtility.UrlEncode("%7bEMAILADDRESS%7d") %>"/>
The <%= ... %> is used when you need to display an expression.

ASP Page becoming truncated when converting to IHTMLDocument2 using .innerHTML

I'm reading in an .ASP page from a server. The problem I am having is that the page is becoming truncated when I attempted to read the data in by means of the C# code below.
Below is my code accessing the .asp page
var htmlDocument = EmbeddedBrowser.Document as IHTMLDocument2;
var htmlInnerContent = (((HTMLDocument)(htmlDocument)).documentElement).innerHTML;
Below is the .asp page as it sits on my server
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script type="text/javascript">Lots of Java Script Here</script><script type="text/javascript">Lots more Java Script Here</script>
</head>
<body>
<input type="" name="ExpectedClientVersion" value="20.15.09"/>
</body>
</html>
Below is the content of htmlInnerContent after I read it in
<head>
<script type="text/javascript">Lots of Java Script Here</script
</head>
As you can see I am missing the body which is really what I need so I can parse the ExpectedClientVersion.
I have run a fiddler trace and I can see the entire document being passed over.
I also tried researching limitations of .InnerHTML but found only lacking documentation. There may be something to this.
I feel the way I am accessing the document may be incorrect. Does anyone have insight into this?

How to add <link> or <meta> tags to <head> with HtmlAgilityPack?

The link to download documentation from http://htmlagilitypack.codeplex.com is returning an error and I can't figure this out by trying the code.
I'm trying to insert various tags into the <head> section of a HtmlDocument that I've loaded from a HTML string. The original issue I'm having is described here.
Can somebody give me an idea of how to achieve this? Thanks
Maybe a bit late :-) Suppose I have this test.htm Html file:
<html>
<head>
<title>Hello World!</title>
</head>
<body>
Hello World
</body>
</html>
Here is how to add a LINK element underneath the HEAD element. You will not the semantics is very close to the System.Xml one, on purpose:
HtmlDocument doc = new HtmlDocument();
doc.Load("test.htm");
HtmlNode head = doc.DocumentNode.SelectSingleNode("/html/head");
HtmlNode link = doc.CreateElement("link");
head.AppendChild(link);
link.SetAttributeValue("rel", "shortcut icon");
link.SetAttributeValue("href", "http://www.mysite.com/favicon.ico");
The result will be:
<html>
<head>
<title>Hello World!</title>
<link rel="shortcut icon" href="http://www.mysite.com/favicon.ico"></head>
<body>
Hello World
</body>
</html>

Categories