保存HtmlDocument &&写入HtmlNode
HtmlDocument - 方法
名称 | 描述 |
---|---|
Save(Stream) | Saves the HTML document to the specified stream. |
Save(StreamWriter) | Saves the HTML document to the specified StreamWriter. |
Save(TextWriter) | Saves the HTML document to the specified TextWriter. |
Save(String) | Saves the mixed document to the specified file. |
Save(XmlWriter) | Saves the HTML document to the specified XmlWriter. |
Save(Stream, Encoding) | Saves the HTML document to the specified stream. |
Save(String, Encoding) | Saves the mixed document to the specified file. |
HtmlNode - 方法
名称 | 描述 |
---|---|
WriteContentTo() | Saves all the children of the node to a string. |
WriteContentTo(TextWriter) | Saves all the children of the node to the specified TextWriter. |
WriteTo() | Saves the current node to a string. |
WriteTo(TextWriter) | Saves the current node to the specified TextWriter. |
WriteTo(XmlWriter) | Saves the current node to the specified XmlWriter. |
public void Save(Stream outStream)
将HTML文档保存到指定的流。 Save
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
outStream: 要保存的流。
以下示例将HTML文档保存到指定的流。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
FileStream sw = new FileStream("FileStream.html", FileMode.Create);
htmlDoc.Save(sw);
public void Save(StreamWriter writer)
将HTML文档保存到指定的StreamWriter
。 Save
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
writer: 您想要保存的StreamWriter。
例子
以下示例将HTML文档保存到指定的StreamWriter。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
StreamWriter sw = new StreamWriter("StreamWriter.html");
htmlDoc.Save(sw);
public void Save(TextWriter writer)
将HTML文档保存到指定的TextWriter
。 Save
方法是* HtmlAgilityPack.HtmlDocument *的成员
参数:
writer: 您想要保存的TextWriter。
以下示例将HTML文档保存到指定的TextWriter
。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
TextWriter tw = File.CreateText("TextWriter.html");
htmlDoc.Save(tw);
public void Save(string filename)
将混合文档保存到指定的文件。Save
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
filename: 要保存文档的文件的位置。
以下示例将混合文档保存到指定的文件。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
htmlDoc.Save("test.html");
public void Save(XmlWriter writer)
将HTML文档保存到指定的XmlWriter
。 Save
方法是HtmlAgilityPack.HtmlDocument的成员
参数:
writer: 你想要保存的XmlWriter
。
以下示例将HTML文档保存到指定的XmlWriter
。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
htmlDoc.OptionOutputAsXml = true;
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
htmlDoc.Save(xw);
public void Save(Stream outStream, Encoding encoding)
使用字符编码将HTML文档保存到指定的流。Save
方法是HtmlAgilityPack.HtmlDocument的成员
参数:
outStream: 要保存的流。 不可能为null。
encoding: 要使用的字符编码。 不可能为null。
以下示例使用字符编码将HTML文档保存到指定的流。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
FileStream sw = new FileStream("FileStream.html", FileMode.Create);
htmlDoc.Save(sw, Encoding.UTF8);
public void Save(string filename, Encoding encoding)
使用字符编码将混合文档保存到指定的文件。 Save
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
filename: 要保存文档的文件的位置。
encoding: 要使用的字符编码。 不可能为null。
例子
以下示例使用字符编码将混合文档保存到指定的文件。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
htmlDoc.Save("test.html", Encoding.UTF8);
public void WriteContentTo()
将节点的所有子节点保存为一个字符串。WriteContentTo
方法是HtmlAgilityPack.HtmlNode 的成员
返回:
保存的字符串。
以下示例将节点的所有子节点保存为一个字符串。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var node = htmlDoc.DocumentNode.SelectSingleNode("//body");
string result = node.WriteContentTo();
public void WriteContentTo(TextWriter outText, [int level = 0])
将节点的所有子节点保存到指定的TextWriter
。 WriteContentTo
方法是HtmlAgilityPack.HtmlNode 的成员
参数:
outText: 您想要保存的TextWriter。
level: 标识从0开始的根级别
以下示例将节点的所有子节点保存为一个字符串。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
TextWriter writer = File.CreateText("TextWriter.html");
var node = htmlDoc.DocumentNode.SelectSingleNode("//body");
node.WriteContentTo(writer);
public string WriteTo()
将当前节点保存为一个字符串。WriteTo
方法是HtmlAgilityPack.HtmlNode 的成员
返回:
保存的字符串。
例子
以下示例将当前节点保存为字符串。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var node = htmlDoc.DocumentNode.SelectSingleNode("//body");
string result = node.WriteTo();
public void WriteTo(TextWriter outText, [int level = 0])
将当前节点保存到指定的TextWriter
。WriteTo
方法是HtmlAgilityPack.HtmlNode 的成员
参数:
outText: 您想要保存的TextWriter。
level: 标识我们以root开始的级别
以下示例将当前节点保存到指定的TextWriter。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
TextWriter writer = File.CreateText("TextWriter.html");
var node = htmlDoc.DocumentNode.SelectSingleNode("//body");
node.WriteTo(writer);
public void WriteTo(XmlWriter writer)
将当前节点保存到指定的XmlWriter
。WriteTo
方法是HtmlAgilityPack.HtmlNode的成员
参数:
writer: 你想要保存的XmlWriter。
以下示例将当前节点保存到指定的XmlWriter。
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var node = htmlDoc.DocumentNode.SelectSingleNode("//body");
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
node.WriteTo(xw);