Skip to content

Commit c87132d

Browse files
Updates
1 parent bee1a3f commit c87132d

File tree

2 files changed

+235
-19
lines changed
  • content/english/net/working-with-html-documents
    • creating-a-document-dotnet-aspose-html
    • editing-a-document-dotnet-aspose-html

2 files changed

+235
-19
lines changed

content/english/net/working-with-html-documents/creating-a-document-dotnet-aspose-html/_index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using Aspose.Html;
3434
## Creating an SVG Document
3535

3636
```csharp
37-
private static void CreateSVG()
37+
static void CreateSVG()
3838
{
3939
using (var document = new SVGDocument("<svg xmlns='http://www.w3.org/2000/svg'><circle cx='50' cy='50' r='40'/></svg>", "about:blank"))
4040
{
@@ -48,7 +48,7 @@ In this example, we create an SVG document by providing the SVG content and a ba
4848
## Creating an HTML Document from Scratch
4949

5050
```csharp
51-
private static void FromScratch()
51+
static void FromScratch()
5252
{
5353
using (var document = new HTMLDocument())
5454
{
@@ -62,7 +62,7 @@ This example demonstrates how to create an HTML document from scratch. The `HTML
6262
## Creating an HTML Document from a Local File
6363

6464
```csharp
65-
private static void FromLocalFile()
65+
static void FromLocalFile()
6666
{
6767
string dataDir = "Your Data Directory";
6868
using (var document = new HTMLDocument(dataDir + "input.html"))
@@ -77,7 +77,7 @@ If you have an existing HTML file on your local system, you can load it using th
7777
## Creating an HTML Document from a Remote URL
7878

7979
```csharp
80-
private static void FromRemoteURL()
80+
static void FromRemoteURL()
8181
{
8282
using (var document = new HTMLDocument("http://your.site.com/"))
8383
{
@@ -91,7 +91,7 @@ Sometimes, you may need to work with HTML content hosted on a remote server. Thi
9191
## Creating an HTML Document from a Remote URL (Alternative)
9292

9393
```csharp
94-
private static void FromRemoteURL1()
94+
static void FromRemoteURL1()
9595
{
9696
using (var document = new HTMLDocument(new Url("http://your.site.com/")))
9797
{
@@ -105,7 +105,7 @@ This alternative approach also shows how to create an HTML document from a remot
105105
## Creating an HTML Document from HTML Content
106106

107107
```csharp
108-
private static void FromHTML()
108+
static void FromHTML()
109109
{
110110
using (var document = new HTMLDocument("<p>my first paragraph</p>", "."))
111111
{
@@ -119,7 +119,7 @@ If you have HTML content in a string format, you can create an HTML document wit
119119
## Creating an HTML Document from a Stream
120120

121121
```csharp
122-
private static void FromStream()
122+
static void FromStream()
123123
{
124124
using (MemoryStream mem = new MemoryStream())
125125
using (StreamWriter sw = new StreamWriter(mem))

content/english/net/working-with-html-documents/editing-a-document-dotnet-aspose-html/_index.md

Lines changed: 228 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,232 @@ weight: 12
88
url: /net/working-with-html-documents/editing-a-document-dotnet-aspose-html/
99
---
1010

11+
Welcome to our tutorial on using Aspose.HTML for .NET, a powerful tool for handling HTML documents in your .NET applications. In this tutorial, we will take you through the essential steps to work with HTML documents using Aspose.HTML. Whether you're a seasoned developer or just starting with .NET development, this guide will help you harness the full potential of Aspose.HTML for your projects.
12+
13+
## Prerequisites
14+
15+
Before we dive into the code examples, make sure you have the following prerequisites in place:
16+
17+
1. **Visual Studio**: You'll need Visual Studio installed on your machine to follow along with the examples.
18+
19+
2. **Aspose.HTML for .NET**: You should have Aspose.HTML for .NET library installed. You can download it from [here](https://releases.aspose.com/html/net/).
20+
21+
3. **A Basic Understanding of C#**: Familiarity with C# programming will be helpful, but even if you're new to C#, you can still follow along and learn.
22+
23+
## Importing Necessary Namespaces
24+
25+
To start using Aspose.HTML for .NET, you need to import the required namespaces. Here's how you can do it:
26+
27+
```csharp
28+
using Aspose.Html;
29+
using Aspose.Html.Dom;
30+
using Aspose.Html.Dom.Css;
31+
```
32+
33+
Now that you have the prerequisites covered let's break down each example into multiple steps and explain each step in detail.
34+
35+
## Example 1: Creating and Editing an HTML Document
36+
37+
```csharp
38+
static void EditDocumentTree()
39+
{
40+
using (var document = new Aspose.Html.HTMLDocument())
41+
{
42+
var body = document.Body;
43+
// Create paragraph element
44+
var p = (Aspose.Html.HTMLParagraphElement)document.CreateElement("p");
45+
// Set custom attribute
46+
p.SetAttribute("id", "my-paragraph");
47+
// Create text node
48+
var text = document.CreateTextNode("my first paragraph");
49+
// Attach text to the paragraph
50+
p.AppendChild(text);
51+
// Attach paragraph to the document body
52+
body.AppendChild(p);
53+
}
54+
}
55+
```
56+
57+
**Explanation:**
58+
59+
1. We start by creating a new HTML document using `Aspose.Html.HTMLDocument()`.
60+
61+
2. We access the document's body element.
62+
63+
3. Next, we create an HTML paragraph element (`<p>`) using `document.CreateElement("p")`.
64+
65+
4. We set a custom attribute `id` for the paragraph element.
66+
67+
5. A text node is created using `document.CreateTextNode("my first paragraph")`.
68+
69+
6. We attach the text node to the paragraph element using `p.AppendChild(text)`.
70+
71+
7. Finally, we attach the paragraph to the document's body.
72+
73+
This example demonstrates how to create and manipulate the structure of an HTML document.
74+
75+
## Example 2: Removing an Element from an HTML Document
76+
77+
```csharp
78+
static void EditDocumentTreeWithAppendRemoveChild()
79+
{
80+
using (var document = new Aspose.Html.HTMLDocument("<p>paragraph</p><div>some element to remove</div>", "about:blank"))
81+
{
82+
var body = document.Body;
83+
// Get "div" element
84+
var div = (Aspose.Html.HTMLDivElement)body.GetElementsByTagName("div").First();
85+
// Remove found element
86+
body.RemoveChild(div);
87+
}
88+
}
89+
```
90+
91+
**Explanation:**
92+
93+
1. We create an HTML document with existing elements, including a `<p>` and a `<div>`.
94+
95+
2. We access the document's body element.
96+
97+
3. Using `body.GetElementsByTagName("div").First()`, we retrieve the first `<div>` element in the document.
98+
99+
4. We remove the selected `<div>` element from the document's body using `body.RemoveChild(div)`.
100+
101+
This example demonstrates how to manipulate and remove elements from an existing HTML document.
102+
103+
## Example 3: Editing HTML Content
104+
105+
```csharp
106+
static void EditHtml()
107+
{
108+
using (var document = new Aspose.Html.HTMLDocument())
109+
{
110+
// Get body element
111+
var body = document.Body;
112+
// Set content of the body element
113+
body.InnerHTML = "<p>paragraph</p>";
114+
// Move to the first child
115+
var node = body.FirstChild;
116+
System.Console.WriteLine(node.LocalName);
117+
}
118+
}
119+
```
120+
121+
**Explanation:**
122+
123+
1. We create a new HTML document.
124+
125+
2. We access the document's body element.
126+
127+
3. Using `body.InnerHTML`, we set the HTML content of the body to `<p>paragraph</p>`.
128+
129+
4. We retrieve the first child element of the body using `body.FirstChild`.
130+
131+
5. We print the local name of the first child element to the console.
132+
133+
This example demonstrates how to set and retrieve the HTML content of an element within an HTML document.
134+
135+
## Example 4: Editing Element Styles
136+
137+
```csharp
138+
static void EditElementStyle()
139+
{
140+
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: red; }</style><p>my first paragraph</p>", "about:blank"))
141+
{
142+
// Get the element to inspect
143+
var element = document.GetElementsByTagName("p")[0];
144+
// Get the CSS view object
145+
var view = (Aspose.Html.Dom.Css.IViewCSS)document.Context.Window;
146+
// Get the computed style of the element
147+
var declaration = view.GetComputedStyle(element);
148+
// Get "color" property value
149+
System.Console.WriteLine(declaration.Color); // rgb(255, 0, 0)
150+
}
151+
}
152+
```
153+
154+
**Explanation:**
155+
156+
1. We create an HTML document with embedded CSS that sets the color of `<p>` elements to red.
157+
158+
2. We retrieve the `<p>` element using `document.GetElementsByTagName("p")[0]`.
159+
160+
3. We access the CSS view object and get the computed style of the `<p>` element.
161+
162+
4. We retrieve and print the value of the "color" property, which is set to red in the CSS.
163+
164+
This example demonstrates how to inspect and manipulate the CSS styles of HTML elements.
165+
166+
## Example 5: Changing Element Style Using Attributes
167+
168+
```csharp
169+
static void EditElementStyleUsingAttribute()
170+
{
171+
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: red; }</style><p>my first paragraph</p>", "about:blank"))
172+
{
173+
// Get the element to edit
174+
var element = (Aspose.Html.HTMLElement)document.GetElementsByTagName("p")[0];
175+
// Get the CSS view object
176+
var view = (Aspose.Html.Dom.Css.IViewCSS)document.Context.Window;
177+
// Get the computed style of the element
178+
var declaration = view.GetComputedStyle(element);
179+
// Set green color
180+
element.Style.Color = "green";
181+
// Get "color" property value
182+
System.Console.WriteLine(declaration.Color); // rgb(0, 128, 0)
183+
}
184+
}
185+
```
186+
187+
**Explanation:**
188+
189+
1. We create an HTML document with embedded CSS that sets the color of `<p>` elements to red.
190+
191+
2. We retrieve the `<p>` element using `document.GetElementsByTagName("p")[0]`.
192+
193+
3. We access the CSS view object and get the computed style of the `<p>` element before any changes.
194+
195+
4. We change the color of the `<p>` element to green using `element.Style.Color = "green"`.
196+
197+
5. We retrieve and print the updated value of the "color"
198+
199+
property, which is now green.
200+
201+
This example demonstrates how to directly modify the style of an HTML element using attributes.
202+
203+
## Conclusion
204+
205+
In this tutorial, we've covered the fundamentals of using Aspose.HTML for .NET to create, manipulate, and style HTML documents within your .NET applications. We explored various examples, from creating an HTML document to editing its structure and styles. With these skills, you can handle HTML documents effectively in your .NET projects.
206+
207+
If you have any questions or need further assistance, don't hesitate to visit the [Aspose.HTML for .NET documentation](https://reference.aspose.com/html/net/) or seek help on the [Aspose forum](https://forum.aspose.com/).
208+
209+
---
210+
211+
## Frequently Asked Questions (FAQs)
212+
213+
1. **What is Aspose.HTML for .NET?**
214+
- Aspose.HTML for .NET is a powerful library for working with HTML documents in .NET applications.
215+
216+
2. **Where can I download Aspose.HTML for .NET?**
217+
- You can download Aspose.HTML for .NET from [here](https://releases.aspose.com/html/net/).
218+
219+
3. **Is there a free trial available?**
220+
- Yes, you can get a free trial of Aspose.HTML from [here](https://releases.aspose.com/).
221+
222+
4. **How can I purchase a license?**
223+
- To purchase a license, visit [this link](https://purchase.aspose.com/buy).
224+
225+
5. **Do I need prior experience with HTML to use Aspose.HTML for .NET?**
226+
- While HTML knowledge is helpful, you can use Aspose.HTML for .NET even if you're not an HTML expert.
227+
228+
## Search Engine Optimized Description
229+
230+
Learn how to work with HTML documents in .NET using Aspose.HTML. This comprehensive tutorial covers document creation, manipulation, and styling. Get started now!
231+
232+
**Note:** Please make sure to check the latest documentation and updates from Aspose.HTML as the library may have evolved since the last knowledge update in September 2021.
11233
## Complete Source Code
12234
```csharp
13-
public static void Run()
14-
{
15-
EditDocumentTree();
16-
EditDocumentTreeWithAppendRemoveChild();
17-
EditHtml();
18-
EditElementStyleUsingAttribute();
19-
}
20-
private static void EditDocumentTree()
235+
236+
static void EditDocumentTree()
21237
{
22238
using (var document = new Aspose.Html.HTMLDocument())
23239
{
@@ -34,7 +250,7 @@ url: /net/working-with-html-documents/editing-a-document-dotnet-aspose-html/
34250
body.AppendChild(p);
35251
}
36252
}
37-
private static void EditDocumentTreeWithAppendRemoveChild()
253+
static void EditDocumentTreeWithAppendRemoveChild()
38254
{
39255
using (var document = new Aspose.Html.HTMLDocument("<p>paragraph</p><div>some element to remove</div>", "about:blank"))
40256
{
@@ -45,7 +261,7 @@ url: /net/working-with-html-documents/editing-a-document-dotnet-aspose-html/
45261
body.RemoveChild(div);
46262
}
47263
}
48-
private static void EditHtml()
264+
static void EditHtml()
49265
{
50266
using (var document = new Aspose.Html.HTMLDocument())
51267
{
@@ -58,7 +274,7 @@ url: /net/working-with-html-documents/editing-a-document-dotnet-aspose-html/
58274
System.Console.WriteLine(node.LocalName);
59275
}
60276
}
61-
private static void EditElementStyle()
277+
static void EditElementStyle()
62278
{
63279
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: red; }</style><p>my first paragraph</p>", "about:blank"))
64280
{
@@ -72,7 +288,7 @@ url: /net/working-with-html-documents/editing-a-document-dotnet-aspose-html/
72288
System.Console.WriteLine(declaration.Color); // rgb(255, 0, 0)
73289
}
74290
}
75-
private static void EditElementStyleUsingAttribute()
291+
static void EditElementStyleUsingAttribute()
76292
{
77293
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: red; }</style><p>my first paragraph</p>", "about:blank"))
78294
{

0 commit comments

Comments
 (0)