作者: littleboy
2022-09-12 11:53:21
CSharp编辑Html
创建
程序的执行从 Main 方法开始。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
var document = new HTMLDocument();
var h2 = (HTMLHeadingElement)document.CreateElement("h2");
var text = document.CreateTextNode("This is Sample Heading!");
h2.AppendChild(text);
document.Body.AppendChild(h2);
var p = (HTMLParagraphElement)document.CreateElement("p");
p.SetAttribute("id", "first-paragraph");
var paraText = document.CreateTextNode("This is first paragraph. ");
p.AppendChild(paraText);
document.Body.AppendChild(p);
var list = (HTMLOListElement)document.CreateElement("ol");
var item1 = (HTMLLIElement)document.CreateElement("li"); item1.AppendChild(document.CreateTextNode("First list item."));
var item2 = (HTMLLIElement)document.CreateElement("li"); item2.AppendChild(document.CreateTextNode("Second list item."));
list.AppendChild(item1); list.AppendChild(item2);
document.Body.AppendChild(list);
document.Save(@"C:\Files\html\create-new-document.html");
|
读取
1 2 3 4 5 6 7 8 9
|
string documentPath = @"C:\Files\html\create-new-document.html";
var document = new HTMLDocument(documentPath);
Console.WriteLine(document.DocumentElement.OuterHTML);
|
编辑
参考
C# 读取 HTML 文件 | C# 创建 HTML 文件 |编辑 HTML 文件 C# (aspose.com)