簡體   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