/// <summary> /// Dealing with C unions, we need a two-step marshalling to get an actual instance. /// </summary> /// <param name="nodePointer"></param> /// <returns></returns> private static GumboNode MarshalToSpecificNode(IntPtr nodePointer) { GumboNode node = Marshal.PtrToStructure <GumboNode>(nodePointer); switch (node.type) { case GumboNodeType.GUMBO_NODE_DOCUMENT: return(Marshal.PtrToStructure <GumboDocumentNode>(nodePointer)); case GumboNodeType.GUMBO_NODE_ELEMENT: case GumboNodeType.GUMBO_NODE_TEMPLATE: return(Marshal.PtrToStructure <GumboElementNode>(nodePointer)); case GumboNodeType.GUMBO_NODE_TEXT: case GumboNodeType.GUMBO_NODE_CDATA: case GumboNodeType.GUMBO_NODE_COMMENT: case GumboNodeType.GUMBO_NODE_WHITESPACE: return(Marshal.PtrToStructure <GumboTextNode>(nodePointer)); default: throw new NotImplementedException($"Node type '{node.type}' is not implemented"); } }
static XNode CreateXNode(GumboNode node) { switch (node.type) { case GumboNodeType.GUMBO_NODE_DOCUMENT: return(new XDocument(((GumboDocumentNode)node).GetChildren().Select(CreateXNode))); case GumboNodeType.GUMBO_NODE_ELEMENT: case GumboNodeType.GUMBO_NODE_TEMPLATE: var elementNode = (GumboElementNode)node; var elementName = GetName(elementNode.element.tag); var attributes = elementNode.GetAttributes().Select(x => new XAttribute(NativeUtf8.StringFromNativeUtf8(x.name), NativeUtf8.StringFromNativeUtf8(x.value))); return(new XElement(elementName, attributes, elementNode.GetChildren().Select(CreateXNode))); case GumboNodeType.GUMBO_NODE_TEXT: return(new XText(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); case GumboNodeType.GUMBO_NODE_CDATA: return(new XCData(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); case GumboNodeType.GUMBO_NODE_COMMENT: return(new XComment(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); case GumboNodeType.GUMBO_NODE_WHITESPACE: return(new XText(NativeUtf8.StringFromNativeUtf8(((GumboTextNode)node).text.text))); default: throw new NotImplementedException($"Node type '{node.type}' is not implemented"); } }
public Node(GumboNode node, Node parent) { Type = node.type; ParseFlags = node.parse_flags; Parent = parent; }