2009-05-12 5 views
0

XML 뷰어 컨트롤에 XML 파일을로드하고 편집 한 다음 다시 다른 XML 형식으로 저장하려고합니다.트리 뷰에로드하는 동안 XML 태그 필터링

<MyConfig> 
    <description> 
    <![CDATA[Add All config data]]> 
    </description> 
    <group name="Server"> 
    <description> 
     <![CDATA[Server info]]> 
    </description> 
    <parameter name="Host" type="string"> 
     <description> 
     <![CDATA[Host Name]]> 
     </description> 
     <value>cccc.ac.lk</value> 
    </parameter> 
    <parameter name="Port" type="integer"> 
     <description> 
     <![CDATA[port no]]> 
     </description> 
     <range>0-65536</range> 
     <value>47110</value> 
    </parameter> 
    </group> 
</MyConfig> 

나는 XML 데이터

private void populateTreeControl(System.Xml.XmlNode document, 
System.Windows.Forms.TreeNodeCollection nodes) 
{ 
    foreach (System.Xml.XmlNode node in document.ChildNodes) 
    { 
    string text = (node.Value != null ? node.Value : 
     (node.Attributes != null && node.Attributes.Count > 0) ? 
     node.Attributes[0].Value : node.Name); 
    TreeNode new_child = new TreeNode(text); 
    nodes.Add(new_child); 
    populateTreeControl(node, new_child.Nodes); 
    } 
} 

지금은 일부 노드를 필터링 및 트리 뷰에로드 할

을로드하기 위해 다음과 같은 방법을 사용하고 있습니다. 예를 들어 위의 경우 설명 태그 등을로드하는 것은 쓸모가 없습니다. MyConfig -> 그룹 -> 서버 ---> 호스트 및 MyConfig -> 그룹 -> 서버 ---> 포트로 트리를 만들고 싶습니다.

populateTreeControl() 이 방법은?

답변

1
private void populateTreeControl(
    System.Xml.Node context, 
    System.Windows.Forms.TreeNodeCollection treeNodes, 
    List<string> xpath, 
    int depth 
) 
{ 
    if (xpath.Count > depth) { 
    foreach (System.Xml.XmlNode xmlNode in context.SelectNodes(xpath[depth])) 
    { 
     string text = ""; 
     if (xmlNode.Value != null) 
     text = xmlNode.Value; 
     else if (xmlNode.Attributes.Count > 0) 
     text = xmlNode.Attributes[0].Value; 
     else 
     text = xmlNode.Name; 

     TreeNode new_child = new TreeNode(text); 
     treeNodes.Add(new_child); 

     populateTreeControl(xmlNode, new_child.Nodes, xpath, depth + 1); 
    } 
    } 
} 

전화

List<string> xpath = new List<string>(); 
xpath.Add("/MyConfig/group/[@name='Server']"); 
xpath.Add("parameter[@name='Host' or @name='Port']"); 

populateTreeControl(xmlDoc, tree.Nodes, xpath, 0);