开发者

get the ancestor nodes of a selected xml node using c# into a multiline text box

Im using c#.net. I have an xml file that contains many nodes. I have got the xml file into a tree view. Now when I select a particular node in the treeview, I should be able to display all its ancestors in a multiline 开发者_StackOverflow中文版text box. Please suggest me to do this job.


I´m not really sure what you want but this might be some thing to start with.
The extension method will get the xpath to an XElement node with attributes to specify the exact element more precisely.

public static string ToXPath(this XElement element)
{
    var current = element.Parent;
    string result = "";
    while (current != null)
    {
        string currentDef = current.Name.ToString();
        string attribsDef = "";
        foreach (var attrib in current.Attributes())
        {
            attribsDef += " and @" + attrib.Name + "='" + attrib.Value + "'";
        }
        if (attribsDef.Length > 0)
        {
            currentDef += "[" + attribsDef.Substring(5) + "]";
        }
        result = "/" + currentDef + result;
        current = current.Parent;
    }
    return result.Substring(1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜