繁体   English   中英

使用c#将选定的xml节点的祖先节点放入多行文本框中

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

我正在使用c#.net。 我有一个包含许多节点的xml文件。 我已经将xml文件放入树状视图中。 现在,当我在树视图中选择一个特定的节点时,我应该能够在多行文本框中显示其所有祖先。 请建议我做这项工作。

我不太确定您想要什么,但这可能是一开始的事情。
扩展方法将使xpath到达具有属性的XElement节点,以更精确地指定确切的元素。

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM