本文整理汇总了C#中IDocument.CreateElement方法的典型用法代码示例。如果您正苦于以下问题:C# IDocument.CreateElement方法的具体用法?C# IDocument.CreateElement怎么用?C# IDocument.CreateElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDocument
的用法示例。
在下文中一共展示了IDocument.CreateElement方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateEntries
static IHtmlOrderedListElement CreateEntries(IDocument document, IElement[] headers)
{
var toc = document.CreateElement<IHtmlOrderedListElement>();
// iterate over all elements satisfying the current level in the context
for (var i = 0; i < headers.Length; ++i)
{
// Get the current header
var header = headers[i];
// Create the li entry for the element
var item = CreateEntry(document, header);
// Aggregate possible sub-headers
var subHeaders = new List<IElement>();
// As long as we don't see the original header level we'll collect the sub headers
while (i + subHeaders.Count + 1 < headers.Length &&
headers[i].LocalName != headers[i + subHeaders.Count + 1].LocalName)
{
subHeaders.Add(headers[i + 1 + subHeaders.Count]);
}
// If we collected any sub headers
if (subHeaders.Count > 0)
{
// Get another level of ToC (recursively)
var subToc = CreateEntries(document, subHeaders.ToArray());
// Append the ToC as a child to the current item
item.AppendChild(subToc);
// Also don't forget to skip these entries
i += subHeaders.Count;
}
// Add the current item to the ToC
toc.AppendChild(item);
}
return toc;
}
示例2: CreateEntry
static IHtmlListItemElement CreateEntry(IDocument document, IElement header)
{
// Create a new li element
var item = document.CreateElement<IHtmlListItemElement>();
// Initially the text of the li is the same as the h* text
item.TextContent = header.TextContent;
// Get a potential (named) anchor here
var anchor = header.QuerySelector<IHtmlAnchorElement>("a");
// If there really is an anchor
if (anchor != null)
{
// Get the value of the name attribute
var name = anchor.GetAttribute("name");
// If the attribute exists (!= null) and makes sense (not empty)
if (!String.IsNullOrEmpty(name))
{
// Create a new anchor element
anchor = document.CreateElement<IHtmlAnchorElement>();
// Set its url to the anchor
anchor.Href = "#" + name;
// Set the anchor's text content to the header (= item) text
anchor.TextContent = item.TextContent;
// Replace the text node (= item content) with the anchor
item.ReplaceChild(anchor, item.FirstChild);
}
}
return item;
}
示例3: SimpleTableTest
private static void SimpleTableTest(IDocument document, IElement group, IElement table)
{
var foo1 = group.AppendChild(document.CreateElement("tr")) as IElement;
foo1.Id = "foo";
var bar1 = group.AppendChild(document.CreateElement("tr")) as IElement;
bar1.Id = "bar";
var foo2 = group.AppendChild(document.CreateElement("tr")) as IElement;
foo2.Id = "foo";
var bar2 = group.AppendChild(document.CreateElement("tr")) as IElement;
bar2.Id = "bar";
var rows = (table as IHtmlTableElement).Rows;
Assert.IsNotNull(rows);
CollectionAssert.AreEquivalent(new[] { foo1, bar1, foo2, bar2 }, rows);
Assert.AreEqual(foo1, rows["foo"]);
Assert.AreEqual(bar1, rows["bar"]);
}
示例4: ConsoleForm
public ConsoleForm(IDocument document)
: this(document.Forms.FirstOrDefault() ?? document.CreateElement<IHtmlFormElement>())
{
}