public void BodyNodeType() { //ExStart //ExFor:Body.NodeType //ExFor:HeaderFooter.NodeType //ExFor:Document.FirstSection //ExSummary:Shows how you can enumerate through children of a composite node and detect types of the children nodes. // Open a document Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("Section 1"); builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); builder.Write("Primary header"); builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); builder.Write("Primary footer"); // Get the first section in the document Section section = doc.FirstSection; // A Section is a composite node and therefore can contain child nodes // Section can contain only Body and HeaderFooter nodes foreach (Node node in section) { // Every node has the NodeType property switch (node.NodeType) { case NodeType.Body: { // If the node type is Body, we can cast the node to the Body class Body body = (Body)node; // Write the content of the main story of the section to the console Console.WriteLine("*** Body ***"); Console.WriteLine(body.GetText()); break; } case NodeType.HeaderFooter: { // If the node type is HeaderFooter, we can cast the node to the HeaderFooter class HeaderFooter headerFooter = (HeaderFooter)node; // Write the content of the header footer to the console Console.WriteLine("*** HeaderFooter ***"); Console.WriteLine(headerFooter.HeaderFooterType); Console.WriteLine(headerFooter.GetText()); break; } default: { // Other types of nodes never occur inside a Section node throw new Exception("Unexpected node type in a section."); } } } //ExEnd }
public void BodyChildNodes() { //ExStart //ExFor:Body.NodeType //ExFor:HeaderFooter.NodeType //ExFor:Document.FirstSection //ExSummary:Shows how to iterate through the children of a composite node. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("Section 1"); builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); builder.Write("Primary header"); builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); builder.Write("Primary footer"); Section section = doc.FirstSection; // A Section is a composite node and can contain child nodes, // but only if those child nodes are of a "Body" or "HeaderFooter" node type. foreach (Node node in section) { switch (node.NodeType) { case NodeType.Body: { Body body = (Body)node; Console.WriteLine("Body:"); Console.WriteLine($"\t\"{body.GetText().Trim()}\""); break; } case NodeType.HeaderFooter: { HeaderFooter headerFooter = (HeaderFooter)node; Console.WriteLine($"HeaderFooter type: {headerFooter.HeaderFooterType}:"); Console.WriteLine($"\t\"{headerFooter.GetText().Trim()}\""); break; } default: { throw new Exception("Unexpected node type in a section."); } } } //ExEnd }