/// <summary> /// 将文档中所有的uri属性转换为绝对的uri。 /// </summary> /// <param name="document">要执行转换的文档</param> /// <param name="resolveInternalReference">是否转换页内 uri</param> public static void ResolveUriToAbsoluate(this IHtmlDocument document, bool resolveInternalReference) { if (document == null) { throw new ArgumentNullException("document"); } var baseUri = document.DocumentUri; if (baseUri == null) { throw new InvalidOperationException(); } foreach (var attribute in document.AllElements().SelectMany(e => e.Attributes()).Where(a => document.HtmlSpecification.IsUriValue(a)).ToArray()) { var value = attribute.Value(); if (string.IsNullOrEmpty(value)) { continue; } Uri uri; if (!Uri.TryCreate(baseUri, value, out uri)) { continue; } if (uri.Equals(baseUri) && !resolveInternalReference) { continue; } attribute.Element.SetAttribute(attribute.Name, uri.AbsoluteUri); } }