C#/C# WPF 개념

[C# WPF] 트리 구조에서 클릭한 최하위 노드의 파일의 경로 가져오기

냠냠쿠 2023. 9. 15. 08:21
728x90

 

TreePanel.xaml

        //자식노드가 있는지 확인 ( 이름에 .xml이 들어가면 가장 하위노드임)
        public bool IsFileNode(TreeViewItem item)
        {
            return item.Header.ToString().EndsWith(".xml");
        }

		
        //부모트리 ~ root 트리까지 타고올라가서 경로 추가하기
        public string GetFilePathFromTree(TreeViewItem item)
        {
            string path = "Models\\";
            string filePath = "";

            while (item != null)

            {
                // 각 노드의 헤더 문자열을 경로에 추가
                filePath = Path.Combine(item.Header.ToString(), filePath);
                item = item.Parent as TreeViewItem;
            }
            return path+filePath;
        }


        //쿼리내용 가져오기
       public string getQueryContent(TreeViewItem item) 
        {
            string tmp = System.IO.Directory.GetCurrentDirectory().Replace("bin\\Debug","");
            string QueryContent=null;
            string QueryPath = tmp+GetFilePathFromTree(item);
            string queryFileName = item.Header.ToString().Replace(".xml","");

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(QueryPath);

            // xml에서 쿼리값 가져오기 
            XmlNode ctg_node = xmlDoc.SelectSingleNode(queryFileName);
            XmlNode name_node = xmlDoc.SelectSingleNode(queryFileName + "/Query");
            QueryContent = name_node.InnerText;
            return QueryContent;
        }
    }

 

- TreeViewModel.cs

        //쿼리내용 가져오기
       public string getQueryContent(TreeViewItem item) 
        {
            string tmp = System.IO.Directory.GetCurrentDirectory().Replace("bin\\Debug","");
            string QueryContent=null;
            string QueryPath = tmp+GetFilePathFromTree(item);
            string queryFileName = item.Header.ToString().Replace(".xml","");

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(QueryPath);

            // xml에서 쿼리값 가져오기 
            XmlNode ctg_node = xmlDoc.SelectSingleNode(queryFileName);
            XmlNode name_node = xmlDoc.SelectSingleNode(queryFileName + "/Query");
            QueryContent = name_node.InnerText;
            return QueryContent;
        }

 

 

728x90