HtmlDocument vb net

Nói chung trong khi thiết kế CMS chúng tôi thấy tình huống này cần xử lý. Giả sử chúng ta có các bản ghi trong Cơ sở dữ liệu. Từ những bản ghi đó, chúng tôi yêu cầu Tạo các trang HTML động. Ở đây tôi đã làm như vậy bằng cách sử dụng VB. NET hoạt động tập tin

Nhìn vào các Mã bên dưới từ Mã phía sau tệp của tôi. Ở đây mình có 2 chức năng & sự kiện tải trang. Trong quá trình tải trang, tôi đang gọi hàm CreatePage(). Hàm CreatePage chấp nhận 3 tham số tiêu đề trang, văn bản HTML ở đây ý tôi là nội dung cho trang HTML & Tên tệp tôi muốn tạo vật lý

Bên trong hàm CreatePage Tôi đang nối một chuỗi có định dạng HTML. Trong thẻ đầu tôi đang thêm tiêu đề trang. Sau đó nhập một ký tự dòng mới. Tương tự như vậy đối với tiêu đề và nội dung cơ thể. Không thực hiện Thao tác tệp ở đây. Để thao tác với tệp, tôi có thêm một chức năng riêng biệt SaveTextToFile(). SaveTextToFile chấp nhận 2 tham số. Tham số đầu tiên là chuỗi HTML mà chúng ta nối trong hàm CreatePage. Thứ hai là đường dẫn tập tin. Đường dẫn tệp có nghĩa là nơi tạo trang HTML

Để tạo tệp vật lý ở đây, tôi đã giới thiệu Hệ thống. Phương thức IO & StreamWriter. Sau khi tạo sơ thẩm, tôi đang sử dụng phương thức Viết. Bao gồm cả điều này tôi có một biến boolean. Điều này giúp biết liệu tệp được tạo thành công hay không. Giá trị mặc định của biến này là sai. Ngay sau phương thức Ghi StreamWriter, tôi đang cập nhật biến này

Essential DocIO chuyển đổi tệp HTML thành tài liệu Word và ngược lại. Bạn cũng có thể chuyển đổi tài liệu HTML sang định dạng văn bản thuần túy và ngược lại

Trong thư viện Word (DocIO), chúng tôi sử dụng XmlReader để phân tích nội dung từ HTML đầu vào. Vì vậy, HTML đầu vào phải đáp ứng tiêu chuẩn XML (có các thẻ mở và đóng phù hợp), ngay cả khi bạn chỉ định tham số XHTMLValidationType là XHTMLValidationType. Không có

Mọi nội dung HTML đều được xác thực theo Tuyên bố loại tài liệu (DTD) là một tập hợp các khai báo đánh dấu xác định loại tài liệu cho ngôn ngữ đánh dấu họ SGML (GML, SGML, XML, HTML)

Các loại xác thực XHTML sau đây được hỗ trợ trong Essential DocIO khi nhập nội dung HTML

Có thể tải xuống một ví dụ hoạt động hoàn chỉnh về việc chuyển đổi tài liệu HTML thành văn bản thuần túy trong C# từ đây

Hãy dành một chút thời gian để đọc tài liệu, nơi bạn có thể tìm thấy các tùy chọn xử lý tài liệu Word cơ bản cùng với các tính năng như trộn thư, hợp nhất và tách tài liệu, tìm và thay thế văn bản trong tài liệu Word, bảo vệ tài liệu Word và quan trọng nhất là chuyển đổi PDF và Hình ảnh

Ghi chú

Bắt đầu với v16. 2. 0. x, nếu bạn tham khảo các tổ hợp Syncfusion từ thiết lập dùng thử hoặc từ nguồn cấp dữ liệu NuGet, hãy bao gồm khóa cấp phép trong dự án của bạn. Tham khảo liên kết để tìm hiểu về cách tạo và đăng ký mã cấp phép Syncfusion trong ứng dụng của bạn để sử dụng các thành phần mà không có thông báo theo dõi

Tôi đang tạo một phần mềm cho bạn gái của tôi, khi cô ấy khởi chạy nó, nó sẽ tự động lấy một dòng văn bản từ một trang web tử vi và hiển thị nó trong TextBox để cô ấy đọc

Tôi không muốn nó thực tế hiển thị toàn bộ trang trong HTML như hiện tại. Tôi cần sao chép dòng HTML ở đây

"You might have the desire for travel, perhaps to visit a friend who lives far away, Gemini. You may actually set the wheels in motion to make it happen. Social events could take up your time this evening, and you could meet some interesting people. A friend might need a sympathetic ear. Today you're especially sensitive to others, so be prepared to hear a sad story. Otherwise, your day should go well.

Hiện mình đang có mã này

Imports System.Net
Imports System.IO
Imports HtmlAgilityPack

Public Class Form1

    Private Function getHTML(ByVal Address As String) As String
        Dim rt As String = ""

        Dim wRequest As WebRequest
        Dim wResponse As WebResponse

        Dim SR As StreamReader

        wRequest = WebRequest.Create(Address)
        wResponse = wRequest.GetResponse

        SR = New StreamReader(wResponse.GetResponseStream)

        rt = SR.ReadToEnd
        SR.Close()

        Return rt
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label2.Text = Date.Now.ToString("MM/dd/yyyy")
        TextBox1.Text = getHTML("http://my.horoscope.com/astrology/free-daily-horoscope-gemini.html")
    End Sub
End Class

Tôi đánh giá cao bất kỳ sự trợ giúp nào bạn có thể cung cấp cho tôi. Trân trọng, tôi không biết lấy phần mềm này ở đâu vào thời điểm này. Ba ngày đã trôi qua mà không có tiến triển gì

Ở đây chúng tôi sẽ chỉ cho bạn hai cách để tạo cùng một tài liệu HTML từ đầu. Sử dụng bất kỳ trong số chúng, cái nào rõ ràng hơn với bạn

DocumentBuilder là một lớp mạnh mẽ được liên kết với DocumentCore và cho phép xây dựng tài liệu động từ đầu hoặc thêm các phần tử mới vào tài liệu hiện có

Mọi thứ có thể với DocumentBuilder cũng có thể thực hiện được khi sử dụng trực tiếp các lớp của DOM (Mô hình đối tượng tài liệu), nhưng thường yêu cầu nhiều dòng mã hơn so với sử dụng DocumentBuilder

DOM (Mô hình đối tượng tài liệu) rất đơn giản và rõ ràng, đó là biểu diễn trong bộ nhớ của tài liệu Word

using SautinSoft.Document;
using SautinSoft.Document.Drawing;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Here we'll show two ways to create HTML document from a scratch.
            // Use any of them, which is more clear to you.

            // 1. With help of DocumentBuilder (wizard).
            CreateHtmlUsingDocumentBuilder();

            // 2. With Document Object Model (DOM) directly.
            CreateHtmlUsingDOM();
        }
        /// 
        /// Creates a new HTML document using DocumentBuilder wizard.
        /// 
        /// 
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-html-document-net-csharp-vb.php
        /// 
        public static void CreateHtmlUsingDocumentBuilder()
        {
            // Set a path to our document.
            string docPath = @"Result-DocumentBuilder.html";

            // Create a new document and DocumentBuilder.
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            // Set page size A4.
            Section section = db.Document.Sections[0];
            section.PageSetup.PaperType = PaperType.A4;

            // Add 1st paragraph with formatted text.
            db.CharacterFormat.FontName = "Verdana";
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = Color.Orange;
            db.Write("This is a first line in 1st paragraph!");
            // Add a line break into the 1st paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            // Add 2nd line to the 1st paragraph, create 2nd paragraph.
            db.Writeln("Let's type a second line.");
            // Specify the paragraph alignment.
            (section.Blocks[0] as Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center;

            // Add text into the 2nd paragraph.
            db.CharacterFormat.ClearFormatting();
            db.CharacterFormat.Size = 25;
            db.CharacterFormat.FontColor = Color.Blue;
            db.CharacterFormat.Bold = true;
            db.Write("This is a first line in 2nd paragraph.");
            // Insert a line break into the 2nd paragraph.
            db.InsertSpecialCharacter(SpecialCharacterType.LineBreak);
            // Insert 2nd line with own formatting to the 2nd paragraph.
            db.CharacterFormat.Size = 20;
            db.CharacterFormat.FontColor = Color.DarkGreen;
            db.CharacterFormat.UnderlineStyle = UnderlineType.Single;
            db.CharacterFormat.Bold = false;
            db.Write("This is a second line.");

            // Add a graphics figure into the paragraph.
            db.CharacterFormat.ClearFormatting();
            Shape shape = db.InsertShape(SautinSoft.Document.Drawing.Figure.SmileyFace, new SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter));
            // Specify outline and fill.
            shape.Outline.Fill.SetSolid(new SautinSoft.Document.Color("#358CCB"));
            shape.Outline.Width = 3;
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange);

            // Save the document to the file in HTML format.
            dc.Save(docPath, new HtmlFixedSaveOptions()
            { EmbedImages = true,
            Title="With DocumentBuilder"});

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true });
        }


        /// 
        /// Creates a new HTML document using DOM directly.
        /// 
        /// 
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-html-document-net-csharp-vb.php
        /// 
        public static void CreateHtmlUsingDOM()
        {
            // Set a path to our document.
            string docPath = @"Result-DocumentCore.html";

            // Create a new document.
            DocumentCore dc = new DocumentCore();

            // Add new section.
            Section section = new Section(dc);
            dc.Sections.Add(section);

            // Let's set page size A4.
            section.PageSetup.PaperType = PaperType.A4;

            // Add two paragraphs            
            Paragraph par1 = new Paragraph(dc);
            par1.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            section.Blocks.Add(par1);

            // Let's create a characterformat for text in the 1st paragraph.
            CharacterFormat cf = new CharacterFormat() { FontName = "Verdana", Size = 16, FontColor = Color.Orange };
            Run run1 = new Run(dc, "This is a first line in 1st paragraph!");
            run1.CharacterFormat = cf;
            par1.Inlines.Add(run1);

            // Let's add a line break into the 1st paragraph.
            par1.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            // Copy the formatting.
            Run run2 = run1.Clone();
            run2.Text = "Let's type a second line.";
            par1.Inlines.Add(run2);

            // Add 2nd paragraph.
            Paragraph par2 = new Paragraph(dc, new Run(dc, "This is a first line in 2nd paragraph.", new CharacterFormat() { Size = 25, FontColor = Color.Blue, Bold = true }));
            section.Blocks.Add(par2);
            SpecialCharacter lBr = new SpecialCharacter(dc, SpecialCharacterType.LineBreak);
            par2.Inlines.Add(lBr);
            Run run3 = new Run(dc, "This is a second line.", new CharacterFormat() { Size = 20, FontColor = Color.DarkGreen, UnderlineStyle = UnderlineType.Single });
            par2.Inlines.Add(run3);

            // Add a graphics figure into the paragraph.
            Shape shape = new Shape(dc, new InlineLayout(new SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter)));
            // Specify outline and fill.
            shape.Outline.Fill.SetSolid(new SautinSoft.Document.Color("#358CCB"));
            shape.Outline.Width = 3;
            shape.Fill.SetSolid(SautinSoft.Document.Color.Orange);
            shape.Geometry.SetPreset(Figure.SmileyFace);
            par2.Inlines.Add(shape);

            // Save the document to the file in HTML format.
            dc.Save(docPath, new HtmlFixedSaveOptions()
            { EmbedImages = true,
            Title = "With DOM"
            });

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true });
        }
    }
}

Tải xuống

        
            Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Here we'll show two ways to create HTML document from a scratch.
			' Use any of them, which is more clear to you.

			' 1. With help of DocumentBuilder (wizard).
			CreateHtmlUsingDocumentBuilder()

			' 2. With Document Object Model (DOM) directly.
			CreateHtmlUsingDOM()
		End Sub
		''' 
		''' Creates a new HTML document using DocumentBuilder wizard.
		''' 
		''' 
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-html-document-net-csharp-vb.php
		''' 
		Public Shared Sub CreateHtmlUsingDocumentBuilder()
			' Set a path to our document.
			Dim docPath As String = "Result-DocumentBuilder.html"

			' Create a new document and DocumentBuilder.
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			' Set page size A4.
			Dim section As Section = db.Document.Sections(0)
			section.PageSetup.PaperType = PaperType.A4

			' Add 1st paragraph with formatted text.
			db.CharacterFormat.FontName = "Verdana"
			db.CharacterFormat.Size = 16
			db.CharacterFormat.FontColor = Color.Orange
			db.Write("This is a first line in 1st paragraph!")
			' Add a line break into the 1st paragraph.
			db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)
			' Add 2nd line to the 1st paragraph, create 2nd paragraph.
			db.Writeln("Let's type a second line.")
			' Specify the paragraph alignment.
			TryCast(section.Blocks(0), Paragraph).ParagraphFormat.Alignment = HorizontalAlignment.Center

			' Add text into the 2nd paragraph.
			db.CharacterFormat.ClearFormatting()
			db.CharacterFormat.Size = 25
			db.CharacterFormat.FontColor = Color.Blue
			db.CharacterFormat.Bold = True
			db.Write("This is a first line in 2nd paragraph.")
			' Insert a line break into the 2nd paragraph.
			db.InsertSpecialCharacter(SpecialCharacterType.LineBreak)
			' Insert 2nd line with own formatting to the 2nd paragraph.
			db.CharacterFormat.Size = 20
			db.CharacterFormat.FontColor = Color.DarkGreen
			db.CharacterFormat.UnderlineStyle = UnderlineType.Single
			db.CharacterFormat.Bold = False
			db.Write("This is a second line.")

			' Add a graphics figure into the paragraph.
			db.CharacterFormat.ClearFormatting()
			Dim shape As Shape = db.InsertShape(SautinSoft.Document.Drawing.Figure.SmileyFace, New SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter))
			' Specify outline and fill.
			shape.Outline.Fill.SetSolid(New SautinSoft.Document.Color("#358CCB"))
			shape.Outline.Width = 3
			shape.Fill.SetSolid(SautinSoft.Document.Color.Orange)

			' Save the document to the file in HTML format.
			dc.Save(docPath, New HtmlFixedSaveOptions() With {
				.EmbedImages = True,
				.Title = "With DocumentBuilder"
			})

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docPath) With {.UseShellExecute = True})
		End Sub


		''' 
		''' Creates a new HTML document using DOM directly.
		''' 
		''' 
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-html-document-net-csharp-vb.php
		''' 
		Public Shared Sub CreateHtmlUsingDOM()
			' Set a path to our document.
			Dim docPath As String = "Result-DocumentCore.html"

			' Create a new document.
			Dim dc As New DocumentCore()

			' Add new section.
			Dim section As New Section(dc)
			dc.Sections.Add(section)

			' Let's set page size A4.
			section.PageSetup.PaperType = PaperType.A4

			' Add two paragraphs            
			Dim par1 As New Paragraph(dc)
			par1.ParagraphFormat.Alignment = HorizontalAlignment.Center
			section.Blocks.Add(par1)

			' Let's create a characterformat for text in the 1st paragraph.
			Dim cf As New CharacterFormat() With {
				.FontName = "Verdana",
				.Size = 16,
				.FontColor = Color.Orange
			}
			Dim run1 As New Run(dc, "This is a first line in 1st paragraph!")
			run1.CharacterFormat = cf
			par1.Inlines.Add(run1)

			' Let's add a line break into the 1st paragraph.
			par1.Inlines.Add(New SpecialCharacter(dc, SpecialCharacterType.LineBreak))
			' Copy the formatting.
			Dim run2 As Run = run1.Clone()
			run2.Text = "Let's type a second line."
			par1.Inlines.Add(run2)

			' Add 2nd paragraph.
			Dim par2 As Paragraph = New Paragraph(dc, New Run(dc, "This is a first line in 2nd paragraph.", New CharacterFormat() With {
				.Size = 25,
				.FontColor = Color.Blue,
				.Bold = True
			}))
			section.Blocks.Add(par2)
			Dim lBr As New SpecialCharacter(dc, SpecialCharacterType.LineBreak)
			par2.Inlines.Add(lBr)
			Dim run3 As Run = New Run(dc, "This is a second line.", New CharacterFormat() With {
				.Size = 20,
				.FontColor = Color.DarkGreen,
				.UnderlineStyle = UnderlineType.Single
			})
			par2.Inlines.Add(run3)

			' Add a graphics figure into the paragraph.
			Dim shape As New Shape(dc, New InlineLayout(New SautinSoft.Document.Drawing.Size(50, 50, LengthUnit.Millimeter)))
			' Specify outline and fill.
			shape.Outline.Fill.SetSolid(New SautinSoft.Document.Color("#358CCB"))
			shape.Outline.Width = 3
			shape.Fill.SetSolid(SautinSoft.Document.Color.Orange)
			shape.Geometry.SetPreset(Figure.SmileyFace)
			par2.Inlines.Add(shape)

			' Save the document to the file in HTML format.
			dc.Save(docPath, New HtmlFixedSaveOptions() With {
				.EmbedImages = True,
				.Title = "With DOM"
			})

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Tải xuống

Nếu bạn cần một ví dụ mã mới hoặc có câu hỏi. gửi email cho chúng tôi tại support@sautinsoft. com hoặc hỏi tại Trò chuyện trực tuyến (góc dưới bên phải trang này) hoặc sử dụng Mẫu bên dưới