我们已经使用
一种选择是将输出解析为 C# 代码,然后将其编码为 XML,使每个节点进入string.Format("<{0}>", this.Name);
并string.Format("</{0}>", this._name);
在中间递归地放置所有子节点。
完成此操作后,我将使用查询 XML/HTML 的工具来解析树。成千上万的人已经使用查询选择器和 jQuery 来根据节点之间的关系解析树状结构。我认为这远远优于 TRegex 或其他过时且未维护的 Java 实用程序。
例如,这是回答您的第一个示例:
var xml = CQ.Create(d.ToXml());
//this can be simpler with CSS selectors but I chose Linq since you'll probably find it easier
//Find joe, in our case the node that has the text 'Joe'
var joe = xml["*"].First(x => x.InnerHTML.Equals("Joe"));
//Find the last (deepest) element that answers the critiria that it has "Joe" in it, and has a VBD in it
//in our case the VP
var closestToVbd = xml["*"].Last(x => x.Cq().Has(joe).Has("VBD").Any());
Console.WriteLine("Closest node to VPD:\n " +closestToVbd.OuterHTML);
//If we want the VBD itself we can just find the VBD in that element
Console.WriteLine("\n\n VBD itself is " + closestToVbd.Cq().Find("VBD")[0].OuterHTML);
这是你的第二个例子
//Now for NP closest to 'Shopping', find the element with the text 'shopping' and find it's closest NP
var closest = xml["*"].First(x => x.InnerHTML.Equals("shopping")).Cq()
.Closest("NP")[0].OuterHTML;
Console.WriteLine("\n\n NP closest to shopping is: " + closest);