Skip to content

Commit 4caddbc

Browse files
committed
Updated html document manipulation examples
1 parent 7f5c225 commit 4caddbc

File tree

11 files changed

+1341
-389
lines changed
  • content/english/net/html-document-manipulation
    • creating-a-document-dotnet-aspose-html
    • editing-a-document-dotnet-aspose-html
    • generate-xps-documents-by-xpsdevice-dotnet-aspose-html
    • load-html-doc-asynchronously-dotnet-aspose-html
    • load-html-doc-with-credentials-dotnet-aspose-html
    • load-html-using-remote-server-dotnet-aspose-html
    • load-html-using-url-dotnet-aspose-html
    • merge-html-with-json-dotnet-aspose-html
    • merge-html-with-xml-dotnet-aspose-html
    • saving-a-document-dotnet-aspose-html

11 files changed

+1341
-389
lines changed

content/english/net/html-document-manipulation/_index.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ url: /net/html-document-manipulation/
1010

1111
## HTML Document Manipulation Tutorials
1212
### [Load HTML Documents Asynchronously in .NET with Aspose.HTML](./load-html-doc-asynchronously-dotnet-aspose-html/)
13+
Learn how to use Aspose.HTML for .NET to work with HTML documents. Step-by-step guide with examples and FAQs for developers.
1314
### [Load HTML Documents with Credentials in .NET with Aspose.HTML](./load-html-doc-with-credentials-dotnet-aspose-html/)
15+
Learn how to supercharge your SEO with Aspose.HTML for .NET. Boost rankings, analyze web content, and optimize for search engines.
1416
### [Load HTML Using a Remote Server in .NET with Aspose.HTML](./load-html-using-remote-server-dotnet-aspose-html/)
17+
Unlock the potential of Aspose.HTML for .NET with our comprehensive guide. Learn how to import namespaces, access remote HTML documents, and more.
1518
### [Load HTML Using URL in .NET with Aspose.HTML](./load-html-using-url-dotnet-aspose-html/)
19+
Learn how to harness the power of Aspose.HTML for .NET. Boost your web development with HTML manipulation and rendering.
1620
### [Creating a Document in .NET with Aspose.HTML](./creating-a-document-dotnet-aspose-html/)
21+
Unleash the Power of Aspose.HTML for .NET. Learn to Create, Manipulate, and Optimize HTML and SVG Documents with Ease. Explore Step-By-Step Examples and FAQs.
1722
### [Editing a Document in .NET with Aspose.HTML](./editing-a-document-dotnet-aspose-html/)
23+
Create captivating web content with Aspose.HTML for .NET. Learn how to manipulate HTML, CSS, and more.
1824
### [Saving a Document in .NET with Aspose.HTML](./saving-a-document-dotnet-aspose-html/)
25+
Unlock the power of Aspose.HTML for .NET with our step-by-step guide. Learn to create, manipulate, and convert HTML and SVG documents
1926
### [Merge HTML with Json in .NET with Aspose.HTML](./merge-html-with-json-dotnet-aspose-html/)
27+
Learn to create dynamic and web content using Aspose.HTML for .NET. Empower your online presence and engage your audience.
2028
### [Merge HTML with XML in .NET with Aspose.HTML](./merge-html-with-xml-dotnet-aspose-html/)
21-
### [Generate XPS Documents by XpsDevice in .NET with Aspose.HTML](./generate-xps-documents-by-xpsdevice-dotnet-aspose-html/)
29+
Learn to use Aspose.HTML for .NET. Import namespace, merge HTML with XML, and enhance your web development skills with this comprehensive guide.
30+
### [Generate XPS Documents by XpsDevice in .NET with Aspose.HTML](./generate-xps-documents-by-xpsdevice-dotnet-aspose-html/)
31+
Unlock the potential of web development with Aspose.HTML for .NET. Create, convert, and manipulate HTML documents easily.

content/english/net/html-document-manipulation/creating-a-document-dotnet-aspose-html/_index.md

Lines changed: 225 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,120 +2,235 @@
22
title: Creating a Document in .NET with Aspose.HTML
33
linktitle: Creating a Document in .NET with Aspose.HTML
44
second_title: Aspose.Slides .NET HTML manipulation API
5-
description:
5+
description: Unleash the Power of Aspose.HTML for .NET. Learn to Create, Manipulate, and Optimize HTML and SVG Documents with Ease. Explore Step-By-Step Examples and FAQs.
66
type: docs
77
weight: 14
88
url: /net/html-document-manipulation/creating-a-document-dotnet-aspose-html/
99
---
1010

11-
## Complete Source Code
11+
In the ever-evolving world of web development, staying ahead of the curve is essential. Aspose.HTML for .NET provides developers with a robust toolkit to work with HTML documents. Whether you're starting from scratch, loading from a file, pulling from a URL, or handling SVG documents, this library offers the versatility you need.
12+
13+
In this step-by-step guide, we will delve into the fundamentals of using Aspose.HTML for .NET, ensuring you're well-equipped to wield this powerful tool in your web development projects. Before we dive into the details, let's go over the prerequisites and the necessary namespaces to kickstart your journey.
14+
15+
## Prerequisites
16+
17+
To successfully follow this tutorial and harness the power of Aspose.HTML for .NET, you'll need the following prerequisites:
18+
19+
- A Windows machine with .NET Framework or .NET Core installed.
20+
- A code editor like Visual Studio.
21+
- Basic knowledge of C# programming.
22+
23+
Now that you have your prerequisites in place, let's get started.
24+
25+
## Importing Namespaces
26+
27+
Before you start using Aspose.HTML for .NET, you need to import the necessary namespaces. These namespaces contain classes and methods that are vital for working with HTML documents. Below is a list of namespaces you should import:
28+
29+
```csharp
30+
using Aspose.Html;
31+
using Aspose.Html.Dom.Svg;
32+
```
33+
34+
With these namespaces imported, you are now ready to dive into the step-by-step examples.
35+
36+
## Creating an HTML Document from Scratch
37+
38+
### Step 1: Initialize an Empty HTML Document
39+
40+
```csharp
41+
// Initialize an empty HTML Document.
42+
using (var document = new Aspose.Html.HTMLDocument())
43+
{
44+
// Create a text element and add it to the document
45+
var text = document.CreateTextNode("Hello World!");
46+
document.Body.AppendChild(text);
47+
// Save the document to disk.
48+
document.Save("document.html");
49+
}
50+
```
51+
52+
In this example, we start by creating an empty HTML document and adding a "Hello World!" text to it. We then save the document to a file.
53+
54+
## Creating an HTML Document from a File
55+
56+
### Step 1: Prepare a 'document.html' file
57+
58+
```csharp
59+
System.IO.File.WriteAllText("document.html", "Hello World!");
60+
```
61+
62+
### Step 2: Load from a 'document.html' file
63+
1264
```csharp
13-
public static void HTMLDocumentFromScratch()
14-
{
15-
// Initialize an empty HTML Document.
16-
using (var document = new Aspose.Html.HTMLDocument())
17-
{
18-
// Create a text element and add it to the document
19-
var text = document.CreateTextNode("Hello World!");
20-
document.Body.AppendChild(text);
21-
// Save the document to disk.
22-
document.Save("document.html");
23-
}
24-
}
25-
public static void HTMLDocumentFromFile()
26-
{
27-
// Prepare a 'document.html' file.
28-
System.IO.File.WriteAllText("document.html", "Hello World!");
29-
// Load from a 'document.html' file.
30-
using (var document = new Aspose.Html.HTMLDocument("document.html"))
31-
{
32-
// Write the document content to the output stream.
33-
Console.WriteLine(document.DocumentElement.OuterHTML);
34-
}
35-
}
36-
public static void HTMLDocumentFromURL()
37-
{
38-
// Load a document from 'https://html.spec.whatwg.org/multipage/introduction.html' web page
39-
using (var document = new Aspose.Html.HTMLDocument("https://html.spec.whatwg.org/multipage/introduction.html"))
40-
{
41-
// Write the document content to the output stream.
42-
Console.WriteLine(document.DocumentElement.OuterHTML);
43-
}
44-
}
45-
public static void HTMLDocumentFromString()
46-
{
47-
// Prepare an HTML code
48-
var html_code = "<p>Hello World!</p>";
49-
// Initialize document from the string variable
50-
using (var document = new Aspose.Html.HTMLDocument(html_code, "."))
51-
{
52-
// Save the document to disk.
53-
document.Save("document.html");
54-
}
55-
}
56-
public static void HTMLDocumentFromMemoryStream()
57-
{
58-
// Create a memory stream object
59-
using (var mem = new System.IO.MemoryStream())
60-
using (var sw = new System.IO.StreamWriter(mem))
61-
{
62-
// Write the HTML Code into memory object
63-
sw.Write("<p>Hello World!</p>");
64-
// It is important to set the position to the beginning, since HTMLDocument starts the reading exactly from the current position within the stream.
65-
sw.Flush();
66-
mem.Seek(0, System.IO.SeekOrigin.Begin);
67-
// Initialize document from the string variable
68-
using (var document = new Aspose.Html.HTMLDocument(mem, "."))
69-
{
70-
// Save the document to disk.
71-
document.Save("document.html");
72-
}
73-
}
74-
}
75-
public static void SVGDocumentFromString()
76-
{
77-
// Initialize the SVG Document from the string object
78-
using (var document = new Aspose.Html.Dom.Svg.SVGDocument("<svg xmlns='http://www.w3.org/2000/svg'><circle cx='50' cy='50' r='40'/></svg>", "."))
79-
{
80-
// Write the document content to the output stream.
81-
Console.WriteLine(document.DocumentElement.OuterHTML);
82-
}
83-
}
84-
public static void HTMLDocumentAsynchronouslyOnReadyStateChange()
85-
{
86-
// Create the instance of HTML Document
87-
var document = new Aspose.Html.HTMLDocument();
88-
// Subscribe to the 'ReadyStateChange' event.
89-
// This event will be fired during the document loading process.
90-
document.OnReadyStateChange += (sender, @event) =>
91-
{
92-
// Check the value of 'ReadyState' property.
93-
// This property is representing the status of the document. For detail information please visit https://www.w3schools.com/jsref/prop_doc_readystate.asp
94-
if (document.ReadyState == "complete")
95-
{
96-
Console.WriteLine(document.DocumentElement.OuterHTML);
97-
Console.WriteLine("Loading is completed. Press any key to continue...");
98-
}
99-
};
100-
// Navigate asynchronously at the specified Uri
101-
document.Navigate("https://html.spec.whatwg.org/multipage/introduction.html");
102-
Console.WriteLine("Waiting for loading...");
103-
Console.ReadLine();
104-
}
105-
public static void HTMLDocumentAsynchronouslyOnLoad()
106-
{
107-
// Create the instance of HTML Document
108-
var document = new Aspose.Html.HTMLDocument();
109-
// Subscribe to the 'OnLoad' event.
110-
// This event will be fired once the document is fully loaded.
111-
document.OnLoad += (sender, @event) =>
112-
{
113-
Console.WriteLine(document.DocumentElement.OuterHTML);
114-
Console.WriteLine("Loading is completed. Press any key to continue...");
115-
};
116-
// Navigate asynchronously at the specified Uri
117-
document.Navigate("https://html.spec.whatwg.org/multipage/introduction.html");
118-
Console.WriteLine("Waiting for loading...");
119-
Console.ReadLine();
120-
}
65+
using (var document = new Aspose.Html.HTMLDocument("document.html"))
66+
{
67+
// Write the document content to the output stream.
68+
Console.WriteLine(document.DocumentElement.OuterHTML);
69+
}
12170
```
71+
72+
Here, we prepare a file with "Hello World!" content and then load it as an HTML document. We print the document's content to the console.
73+
74+
## Creating an HTML Document from a URL
75+
76+
### Step 1: Load a document from a web page
77+
78+
```csharp
79+
using (var document = new Aspose.Html.HTMLDocument("https://html.spec.whatwg.org/multipage/introduction.html"))
80+
{
81+
// Write the document content to the output stream.
82+
Console.WriteLine(document.DocumentElement.OuterHTML);
83+
}
84+
```
85+
86+
In this example, we load an HTML document directly from a web page and display its content.
87+
88+
## Creating an HTML Document from a String
89+
90+
### Step 1: Prepare an HTML code
91+
92+
```csharp
93+
var html_code = "<p>Hello World!</p>";
94+
```
95+
96+
### Step 2: Initialize document from the string variable
97+
98+
```csharp
99+
using (var document = new Aspose.Html.HTMLDocument(html_code, "."))
100+
{
101+
// Save the document to disk.
102+
document.Save("document.html");
103+
}
104+
```
105+
106+
Here, we create an HTML document from a string variable and save it to a file.
107+
108+
## Creating an HTML Document from a MemoryStream
109+
110+
### Step 1: Create a memory stream object
111+
112+
```csharp
113+
using (var mem = new System.IO.MemoryStream())
114+
using (var sw = new System.IO.StreamWriter(mem))
115+
{
116+
// Write the HTML Code into the memory object
117+
sw.Write("<p>Hello World!</p>");
118+
// Set the position to the beginning
119+
sw.Flush();
120+
mem.Seek(0, System.IO.SeekOrigin.Begin);
121+
// Initialize document from the memory stream
122+
using (var document = new Aspose.Html.HTMLDocument(mem, "."))
123+
{
124+
// Save the document to disk.
125+
document.Save("document.html");
126+
}
127+
}
128+
```
129+
130+
In this example, we create an HTML document from a memory stream and save it to a file.
131+
132+
## Working with SVG Documents
133+
134+
### Step 1: Initialize the SVG Document from a string
135+
136+
```csharp
137+
using (var document = new Aspose.Html.Dom.Svg.SVGDocument("<svg xmlns='http://www.w3.org/2000/svg'><circle cx='50' cy='50' r='40'/></svg>", "."))
138+
{
139+
// Write the document content to the output stream.
140+
Console.WriteLine(document.DocumentElement.OuterHTML);
141+
}
142+
```
143+
144+
Here, we create and display an SVG document from a string.
145+
146+
## Loading an HTML Document Asynchronously
147+
148+
### Step 1: Create the instance of HTML Document
149+
150+
```csharp
151+
var document = new Aspose.Html.HTMLDocument();
152+
```
153+
154+
### Step 2: Subscribe to the 'ReadyStateChange' event
155+
156+
```csharp
157+
document.OnReadyStateChange += (sender, @event) =>
158+
{
159+
// Check the value of 'ReadyState' property.
160+
if (document.ReadyState == "complete")
161+
{
162+
Console.WriteLine(document.DocumentElement.OuterHTML);
163+
Console.WriteLine("Loading is completed. Press any key to continue...");
164+
}
165+
};
166+
```
167+
168+
### Step 3: Navigate asynchronously at the specified Uri
169+
170+
```csharp
171+
document.Navigate("https://html.spec.whatwg.org/multipage/introduction.html");
172+
Console.WriteLine("Waiting for loading...");
173+
Console.ReadLine();
174+
```
175+
176+
In this example, we load an HTML document asynchronously and handle the 'ReadyStateChange' event to display the content when loading is complete.
177+
178+
## Handling the 'OnLoad' Event
179+
180+
### Step 1: Create the instance of HTML Document
181+
182+
```csharp
183+
var document = new Aspose.Html.HTMLDocument();
184+
```
185+
186+
### Step 2: Subscribe to the 'OnLoad' event
187+
188+
```csharp
189+
document.OnLoad += (sender, @event) =>
190+
{
191+
Console.WriteLine(document.DocumentElement.OuterHTML);
192+
Console.WriteLine("Loading is completed. Press any key to continue...");
193+
};
194+
```
195+
196+
### Step 3: Navigate asynchronously at the specified Uri
197+
198+
```csharp
199+
document.Navigate("https://html.spec.whatwg.org/multipage/introduction.html");
200+
Console.WriteLine("Waiting for loading...");
201+
Console.ReadLine();
202+
```
203+
204+
This example demonstrates loading an HTML document asynchronously and handling the 'OnLoad' event to display the content upon completion.
205+
206+
## In Conclusion
207+
208+
In the dynamic world of web development, having the right tools at your disposal is crucial. Aspose.HTML for .NET equips you with the means to create, manipulate, and process HTML and SVG documents efficiently. This comprehensive guide has walked you through the essentials, ensuring that you can harness the power of Aspose.HTML for .NET in your projects.
209+
210+
## FAQ's
211+
212+
### Q1: What is Aspose.HTML for .NET?
213+
214+
A1: Aspose.HTML for .NET is a powerful .NET library that enables developers to work with HTML and SVG documents. It provides a wide range of features, from creating documents from scratch to parsing and manipulating existing HTML and SVG files.
215+
216+
### Q2: Can I use Aspose.HTML for .NET with .NET Core?
217+
218+
A2: Yes, Aspose.HTML for .NET is compatible with both .NET Framework and .NET Core, making it a versatile choice for modern .NET applications.
219+
220+
### Q3: Is Aspose.HTML for .NET suitable for web scraping and parsing?
221+
222+
A3: Absolutely! Aspose.HTML for .NET is an excellent choice for web scraping and parsing tasks, thanks to its ability to load HTML documents from URLs and strings. You can extract data, perform analysis, and more.
223+
224+
### Q4: How can I access support for Aspose.HTML for .NET?
225+
226+
A4: If you encounter any issues or have questions while using Aspose.HTML for .NET, you can visit the [Aspose Forum](https://forum.aspose.com/) for support and assistance from the community and Aspose experts.
227+
228+
### A5: Where can I find detailed documentation and download options?
229+
230+
A5: For comprehensive documentation and access to download options, you can refer to the following links:
231+
232+
- [Documentation](https://reference.aspose.com/html/net/)
233+
- [Download](https://releases.aspose.com/html/net/)
234+
- [Buy](https://purchase.aspose.com/buy)
235+
- [Free Trial](https://releases.aspose.com/)
236+
- [Temporary License](https://purchase.aspose.com/temporary-license/)

0 commit comments

Comments
 (0)