当前位置: 首页>>代码示例>>C#>>正文


C# HtmlNode.WriteContentTo方法代码示例

本文整理汇总了C#中HtmlAgilityPack.HtmlNode.WriteContentTo方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlNode.WriteContentTo方法的具体用法?C# HtmlNode.WriteContentTo怎么用?C# HtmlNode.WriteContentTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HtmlAgilityPack.HtmlNode的用法示例。


在下文中一共展示了HtmlNode.WriteContentTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Save

    /// <summary>
    /// Saves the <see cref="HtmlDocument"/> to the specified path.
    /// </summary>
    /// <param name="path">The file to write to.</param>
    /// <param name="node">The <see cref="HtmlNode"/> to save.</param>
    /// <param name="encoding">The encoding to use when writing the file.</param>
    /// <seealso cref="HtmlNode"/>
    /// <see cref="HtmlDocument"/>
    public static void Save(string path, HtmlNode node, Encoding encoding)
    {
        if (path == null || node == null)
        {
            DebugBreakOrThrow("Figure out why " + (path == null ? "path" : "node") + "is null", new ArgumentNullException(path == null ? "path" : "node"));
        }

        // Will only be triggered if the caller isn't called by another Save().
        if (encoding == null)
        {
            DebugBreakOrThrow("Figure out why encoding is null.", new ArgumentNullException("encoding"));
        }

        using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 1024))
        {
            using (StreamWriter streamWriter = new StreamWriter(fileStream, encoding))
            {
                // StreamWriter is a TextWriter, so we can pass it to HtmlNode.WriteContentTo(TextWriter).
                // So node.WriteTo only saves the current node, which is only useful if the node has no children.
                // node.WriteContentTo, only saves the current nodes children, therefore if the current node has a parent, we use the parent to save.
                // this will include all siblings aswell :S
                if (node.ParentNode != null)
                {
                    node.ParentNode.WriteContentTo(streamWriter);
                }
                else if (node.HasChildNodes == false)
                {
                    node.WriteTo(streamWriter);
                }
                else if (node.Name == HtmlNode.HtmlNodeTypeNameDocument)
                {
                    node.WriteContentTo(streamWriter);
                }
                else
                {
                    // TODO: Properly save parent-less node with children.
                    DebugBreakOrThrow("Properly save parent-less node with children. Inspect 'node'.", new InvalidOperationException("Don't know how to save the node, and it's children!"));
                }

                streamWriter.Flush();
            }
        }
    }
开发者ID:blat001,项目名称:Achievement-Sherpa,代码行数:51,代码来源:HtmlAgilityPackHelper.cs


注:本文中的HtmlAgilityPack.HtmlNode.WriteContentTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。