EMDI는 지금도 개발중

C# : XML을 갖고 TreeView 만들기 How to make TreeView using XML in C# 본문

언어/C#

C# : XML을 갖고 TreeView 만들기 How to make TreeView using XML in C#

EMDI 2019. 5. 16. 17:46

예전에 만들어본 XML을 갖고 트리뷰 만드는 방법입니다. 우선 XML형식으로 된 데이터가 있다는 가정하에 해보도록 하겠습니다. 만약 XML 데이터가 string 형식이면

XmlMemory xml = new XmlMemory(); 해당 생성자를 만든 다음

xml.SetStream(string형식인 데이터); 를 넣어주면 XML 형식으로 만들 수 있습니다.

/// getTreeView 
/// TreeView 첫 부분 생성 
public bool getTreeView(XmlMemory xml, TreeView treeMain) 
{ 
	try 
	{

        // 1. 우선 트리의 노드는 제목을 지칭하는 nodeSection과 값을 지칭하는 nodeEntry를 만들었습니다.
        TreeNode nodeSection, nodeEntry;

        int untilNodes = 0; 
        int sectionCnt = xml.GetSectionNames().Length; 
        for (var i = 0; i < sectionCnt; i++) 
        {

            //2. 노드를 생성하는 방법은 treeMain인 TreeView에 Nodes.Add를 하면 생성할 수 있습니다.
            nodeSection = treeMain.Nodes.Add( 
            getSectionValue(xml, i) 
            , "Section Name [ " + getSectionValue(xml, i) + " ]" 
            , 0, 0); 

            // 0, 0 해당 숫자는 노드의 위치를 지칭합니다.
            //public virtual TreeNode Add(string key, string text, int imageIndex, int selectedImageIndex);
            int entryCnt = xml.GetEntryNames(xml.GetSectionNames()[i]).Length; 
            for (var j = 0; j < entryCnt; j++) 
            { 
                if (getEntryValue(xml, i, j).Contains("xml version")) 
                { 
                	nodeEntry = nodeSection.Nodes.Add( 
                	getEntryName(xml, i, j) 
                	, "Entry Name [ " + getEntryName(xml, i, j) + " ]" 
                	, 1, 1); 
                	untilNodes += 1; 

                	XmlMemory xmlSecond = new XmlMemory(); 
                	xmlSecond.SetStream(getEntryValue(xml, i, j).ToString()); 
                	bool result = getTreeNode(xmlSecond, nodeEntry, untilNodes); 
                    if (result == false) 
                    { 
                    	return false; 
                    } 
                } 
                else 
                { 
                    nodeEntry = nodeSection.Nodes.Add( 
                    getEntryName(xml, i, j) 
                    , "Entry Name [ " + getEntryName(xml, i, j) + " ] " + " (" + getEntryValue(xml, i, j) + ")" 
                    , 1, 1); 
                    
                    untilNodes += 1; 
                } 
            } 
        }// for문 
        return true; 

    } 
    catch (Exception ex) 
    { 
        // 예외 오류 
        Trace.WriteLine(ex.ToString()); 
        return false; 
    } 
}


/// getTreeNode
/// treeView 다음에 받는 treeNode로 재귀함수 반복문
/// 

public bool getTreeNode(XmlMemory xml, TreeNode treeNode, int untilNodes) 
{ 
    try 
    { 
        // untilNodes는 treeNode까지 한 넘버이니 해당 메소드로 들어올 때부터는 +1를 해서 써야한다. 
        untilNodes += 1; 
        TreeNode treeTemp; 
        int sectionCnt = xml.GetSectionNames().Length; 
        
        for (var i = 0; i < sectionCnt; i++) 
        { 
            treeTemp = treeNode.Nodes.Add( 
            getSectionValue(xml, i) 
            , "Data Name [ " + getSectionValue(xml, i) + " ] " 
            , untilNodes, untilNodes); 

        	int entryCnt = xml.GetEntryNames(xml.GetSectionNames()[i]).Length; 
            for (var j = 0; j < entryCnt; j++) 
            { 
                if (getEntryValue(xml, i, j).Contains("xml version")) 
                { 

                    XmlMemory xmlThr = new XmlMemory(); 
                    xmlThr.SetStream(getEntryValue(xml, i, j).ToString()); 
                    bool result = getTreeNode(xmlThr, treeTemp, untilNodes + 1); 
                    if (result == false) 
                    { 
                        return false; 
                    } 
                } 
                else 
                { 
                        treeTemp.Nodes.Add( 
                        getEntryName(xml, i, j) 
                        , getEntryName(xml, i, j) + " (" + getEntryValue(xml, i, j) + ")" 
                        , untilNodes + 1, untilNodes + 1); 
                } 
            } 
        }
        return true; 
    } 
    catch (Exception ex) 
    { 
    // 예외 오류 
    Trace.WriteLine(ex.ToString()); 
    return false; 
    } 

}


// Nodes에 사용할 Section 값 얻기 
public string getSectionValue(XmlMemory xml, int sectionSeq) 
{ 
    string sectionValue = string.Empty; 
    sectionValue = xml.GetSectionNames()[sectionSeq].ToString(); 
    return sectionValue; 
} 

// Nodes에 사용할 Entry 이름명 얻기 
public string getEntryName(XmlMemory xml, int sectionSeq, int entrySeq) 
{ 
    string entryName = string.Empty; 
    entryName = xml.GetEntryNames(xml.GetSectionNames()[sectionSeq])[entrySeq].ToString(); 
    return entryName; 
} 

// Nodes에 사용할 Entry 값 얻기 
public string getEntryValue(XmlMemory xml, int sectionSeq, int entrySeq) 
{ 
    string entryValue = string.Empty; 
    entryValue = xml.GetValue(xml.GetSectionNames()[sectionSeq], xml.GetEntryNames(xml.GetSectionNames()[sectionSeq])[entrySeq]).ToString(); 
    return entryValue; 
}

우선 제가 올린 소스는 section과 entry가 있다는 가정하에 만든 것입니다. XML형식을 어떻게 하면 보기 편하게 TreeView로 변환할 수 있는지에 초점을 맞췄죠. 다음에 시간이 나면 어떠한 XML형식이든 변환할 수 있게 도전해봐야겠네요~

Comments