/// <summary> /// 尝试生成元素开始标签的HTML形式 /// </summary> /// <param name="element">要生成HTML的元素</param> /// <param name="selfClosed">指示是否应产生自结束符号</param> /// <returns></returns> public static string GenerateTagHtml(IHtmlElement element, bool selfClosed) { if (element == null) { throw new ArgumentNullException("element"); } var builder = new StringBuilder(20); builder.Append("<"); builder.Append(element.Name); foreach (var attribute in element.Attributes()) { builder.Append(" "); builder.Append(attribute.Name); if (attribute.AttributeValue != null) { if ((HtmlSpecification.IsUriValue(attribute) || HtmlSpecification.IsScriptValue(attribute)) && !attribute.AttributeValue.Contains('"')) { builder.Append("=\"").Append(attribute.AttributeValue).Append("\""); } else { builder.Append("=\"").Append(HtmlEncoding.HtmlAttributeEncode(attribute.AttributeValue)).Append("\""); } } } if (selfClosed) { builder.Append(" /"); } builder.Append(">"); return(builder.ToString()); }