本文整理汇总了C#中Document.SelectNodes方法的典型用法代码示例。如果您正苦于以下问题:C# Document.SelectNodes方法的具体用法?C# Document.SelectNodes怎么用?C# Document.SelectNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Document
的用法示例。
在下文中一共展示了Document.SelectNodes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
// This a document that we want to add an image and custom text for each page without using the header or footer.
Document doc = new Document(gDataDir + "TestFile.doc");
// Create and attach collector before the document before page layout is built.
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Images in a document are added to paragraphs, so to add an image to every page we need to find at any paragraph
// belonging to each page.
IEnumerator enumerator = doc.SelectNodes("//Body/Paragraph").GetEnumerator();
// Loop through each document page.
for (int page = 1; page <= doc.PageCount; page++)
{
while (enumerator.MoveNext())
{
// Check if the current paragraph belongs to the target page.
Paragraph paragraph = (Paragraph)enumerator.Current;
if (layoutCollector.GetStartPageIndex(paragraph) == page)
{
AddImageToPage(paragraph, page);
break;
}
}
}
doc.Save(gDataDir + "TestFile Out.docx");
}
示例2: ReplaceHyperlinks
[Test] //ExSkip
public void ReplaceHyperlinks()
{
// Specify your document name here.
Document doc = new Document(MyDir + "ReplaceHyperlinks.doc");
// Hyperlinks in a Word documents are fields, select all field start nodes so we can find the hyperlinks.
NodeList fieldStarts = doc.SelectNodes("//FieldStart");
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(FieldType.FieldHyperlink))
{
// The field is a hyperlink field, use the "facade" class to help to deal with the field.
Hyperlink hyperlink = new Hyperlink(fieldStart);
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
if (hyperlink.IsLocal)
continue;
// The Hyperlink class allows to set the target URL and the display name
// of the link easily by setting the properties.
hyperlink.Target = NewUrl;
hyperlink.Name = NewName;
}
}
doc.Save(MyDir + @"\Artifacts\ReplaceHyperlinks.doc");
}
示例3: Run
public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
// This a document that we want to add an image and custom text for each page without using the header or footer.
Document doc = new Document(dataDir + "TestFile.doc");
// Create and attach collector before the document before page layout is built.
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Images in a document are added to paragraphs, so to add an image to every page we need to find at any paragraph
// belonging to each page.
IEnumerator enumerator = doc.SelectNodes("//Body/Paragraph").GetEnumerator();
// Loop through each document page.
for (int page = 1; page <= doc.PageCount; page++)
{
while (enumerator.MoveNext())
{
// Check if the current paragraph belongs to the target page.
Paragraph paragraph = (Paragraph)enumerator.Current;
if (layoutCollector.GetStartPageIndex(paragraph) == page)
{
AddImageToPage(paragraph, page, dataDir);
break;
}
}
}
doc.Save(dataDir + "TestFile Out.docx");
Console.WriteLine("\nInserted images on each page of the document successfully.\nFile saved at " + dataDir + "TestFile Out.docx");
}
示例4: CompositeNode_SelectNodes
public void CompositeNode_SelectNodes()
{
//ExStart
//ExFor:CompositeNode.SelectSingleNode
//ExFor:CompositeNode.SelectNodes
//ExSummary:Shows how to select certain nodes by using an XPath expression.
Document doc = new Document(MyDir + "Table.Document.doc");
// This expression will extract all paragraph nodes which are descendants of any table node in the document.
// This will return any paragraphs which are in a table.
NodeList nodeList = doc.SelectNodes("//Table//Paragraph");
// This expression will select any paragraphs that are direct children of any body node in the document.
nodeList = doc.SelectNodes("//Body/Paragraph");
// Use SelectSingleNode to select the first result of the same expression as above.
Node node = doc.SelectSingleNode("//Body/Paragraph");
//ExEnd
}
示例5: TestNodeIsInsideField
public void TestNodeIsInsideField()
{
//ExStart:
//ExFor:CompositeNode.SelectNodes
//ExFor:CompositeNode.GetChild
//ExSummary:Shows how to test if a node is inside a field by using an XPath expression.
// Let's pick a document we know has some fields in.
Document doc = new Document(MyDir + "MailMerge.MergeImage.doc");
// Let's say we want to check if the Run below is inside a field.
Run run = (Run)doc.GetChild(NodeType.Run, 5, true);
// Evaluate the XPath expression. The resulting NodeList will contain all nodes found inside a field a field (between FieldStart
// and FieldEnd exclusive). There can however be FieldStart and FieldEnd nodes in the list if there are nested fields
// in the path. Currently does not find rare fields in which the FieldCode or FieldResult spans across multiple paragraphs.
NodeList resultList = doc.SelectNodes("//FieldStart/following-sibling::node()[following-sibling::FieldEnd]");
// Check if the specified run is one of the nodes that are inside the field.
foreach (Node node in resultList)
{
if (node == run)
{
Console.WriteLine("The node is found inside a field");
break;
}
}
//ExEnd
}
示例6: GetReferenceFieldContainer
/// <summary>
/// Gets the container for the quote field: the first paragraph in the document.
/// </summary>
private static CompositeNode GetReferenceFieldContainer(Document document)
{
return document.SelectNodes("//Body/Paragraph").OfType<Paragraph>().FirstOrDefault();
}