HtmlAgilityPack.HtmlNode.PrependChild C# (CSharp) Method

PrependChild() public method

Adds the specified node to the beginning of the list of children of this node.
public PrependChild ( HtmlNode newChild ) : HtmlNode
newChild HtmlNode The node to add. May not be null.
return HtmlNode
		public HtmlNode PrependChild(HtmlNode newChild)
		{
			if (newChild == null)
			{
				throw new ArgumentNullException("newChild");
			}
			ChildNodes.Prepend(newChild);
			_ownerdocument.SetIdForNode(newChild, newChild.GetId());
			_outerchanged = true;
			_innerchanged = true;
			return newChild;
		}

Usage Example

    //=========================================================================================
    // 実体のないファイルの場合、画像やリンクは相対アドレスは不可能なので、絶対アドレスへと置き換える。
    //=========================================================================================
    private string RelativeToAbsolute(string html)
    {
        try
        {
            string fn = strCurFileFullPath;
            fn = fn.Replace("\\", "/");
            string strBaseUrl = fn;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);

            bool isExistBaseHref = false;
            foreach (var nodeBase in doc.DocumentNode.Descendants("base"))
            {
                if (nodeBase.Attributes["href"].Value.Length > 0)
                {
                    isExistBaseHref = true;
                }
                else
                {
                }
            }

            // base hrefの指定が無いのであれば、現在の開いているファイルのディレクトリをそこにしておくことで、相対ディレクトリをフォローする。
            if (!isExistBaseHref)
            {
                string basedir = System.IO.Path.GetDirectoryName(strBaseUrl);
                HtmlAgilityPack.HtmlNode baseNode = HtmlAgilityPack.HtmlNode.CreateNode("<base href=''>");
                baseNode.Attributes["href"].Value = basedir + "\\";

                // Headタグがあればそこにたす
                HtmlAgilityPack.HtmlNode nodeHead = doc.DocumentNode.SelectSingleNode("/html/head");
                HtmlAgilityPack.HtmlNode nodeHtml = doc.DocumentNode.SelectSingleNode("/html");
                if (nodeHead != null)
                {
                    nodeHead.PrependChild(baseNode);
                }
                else if (nodeHtml != null)
                {
                    nodeHtml.PrependChild(baseNode);
                }
                // Headタグがないなら、トップにたさざるをえないだろう
                else
                {
                    doc.DocumentNode.PrependChild(baseNode);
                }
            }

            StringWriter writer = new StringWriter();
            doc.Save(writer);

            string newHtml = writer.ToString();
            return(newHtml);
        }
        catch (Exception)
        {
        }

        return(html);
    }
All Usage Examples Of HtmlAgilityPack.HtmlNode::PrependChild