2012-03-26 1 views
0

リンクに "books.xml"というXMLファイルがあります "http://msdn.microsoft.com/en-us/library/ windows/desktop/ms762271(v = vs.85).aspx "となります。ツリービューのノードとして、xml情報から<title>だけを表示させることが私の要求でした。しかし、私は次のすべての値を表示するコードを "カタログ"のようなルートノードとして、親としてすべての著者、タイトル、ジャンルなどのノードとして表示するコーディングを行ったが、私はルートノードのカタログとタイトルだけノード。すべてのボディは、私はあなたの意図は、タイトルやカテゴリ・ノードの下に何もを表示するだけであると仮定しツリービューでノードとして選択されたものだけを表示するためにXMLファイルからデータをフィルタリングする方法

OpenFileDialog dlg = new OpenFileDialog(); 
     dlg.Title = "Open XML document"; 
     dlg.Filter = "XML Files (*.xml)|*.xml"; 
     dlg.FileName = Application.StartupPath + "\\..\\..\\Sample.xml"; 
     if (dlg.ShowDialog() == DialogResult.OK) 
     { 
      try 
      { 
       //Just a good practice -- change the cursor to a 
       //wait cursor while the nodes populate 
       this.Cursor = Cursors.WaitCursor; 
       //First, we'll load the Xml document 
       XmlDocument xDoc = new XmlDocument(); 
       xDoc.Load(dlg.FileName); 
       //Now, clear out the treeview, 
       //and add the first (root) node 
       treeView1.Nodes.Clear(); 
       treeView1.Nodes.Add(new 
        TreeNode(xDoc.DocumentElement.Name)); 
       TreeNode tNode = new TreeNode(); 
       tNode = (TreeNode)treeView1.Nodes[0]; 
       //We make a call to addTreeNode, 
       //where we'll add all of our nodes 
       addTreeNode(xDoc.DocumentElement, tNode); 
       //Expand the treeview to show all nodes 
       treeView1.ExpandAll(); 
      } 
      catch (XmlException xExc) 
      //Exception is thrown is there is an error in the Xml 
      { 
       MessageBox.Show(xExc.Message); 
      } 
      catch (Exception ex) //General exception 
      { 
       MessageBox.Show(ex.Message); 
      } 
      finally 
      { 
       this.Cursor = Cursors.Default; //Change the cursor back 
      } 
     }} 
     //This function is called recursively until all nodes are loaded 
    private void addTreeNode(XmlNode xmlNode, TreeNode treeNode) 
    { 
     XmlNode xNode; 
     TreeNode tNode; 
     XmlNodeList xNodeList; 
     if (xmlNode.HasChildNodes) //The current node has children 
     { 
      xNodeList = xmlNode.ChildNodes; 
      for (int x = 0; x <= xNodeList.Count - 1; x++) 
      //Loop through the child nodes 
      { 
       xNode = xmlNode.ChildNodes[x]; 
       treeNode.Nodes.Add(new TreeNode(xNode.Name)); 
       tNode = treeNode.Nodes[x]; 
       addTreeNode(xNode, tNode); 
      } 
     } 
     else //No children, so add the outer xml (trimming off whitespace) 
      treeNode.Text = xmlNode.OuterXml.Trim(); 
    } 
+0

は[linqtoXML](http://msdn.microsoft.com/en-us/library/bb387098.aspx) – pylover

+0

を試してみてくださいコーディング、それは役に立ちます – michale

答えて

1

をノードとして、私はタイトルを表示するためのexisitngロジックに何をする必要があるか修正私を導くことができます。その場合にはaddTreeNodeメソッドのバージョン以下試してください。

private void addTreeNode(XmlNode xmlNode, TreeNode treeNode) 
    { 
     XmlNode xNode; 
     TreeNode tNode; 
     XmlNodeList xNodeList; 
     if (xmlNode.HasChildNodes && xmlNode.Name != "title") //The current node has children 
     { 
      xNodeList = xmlNode.ChildNodes; 
      for (int x = 0; x <= xNodeList.Count - 1; x++) 
      //Loop through the child nodes 
      { 
       xNode = xmlNode.ChildNodes[x]; 
       //treeNode.Nodes.Add(new TreeNode(xNode.Name)); 
       //tNode = treeNode.Nodes[x]; 
       addTreeNode(xNode, treeNode); 
      } 
     } 
     else if (xmlNode.Name == "title") //No children, so add the outer xml (trimming off whitespace) 
      treeNode.Nodes.Add(new TreeNode(xmlNode.InnerText)); 
    } 

しかし、私は、この目標を達成するために非常に非効率的と無粋な方法であることを強調しなければなりません。あなたが実際に以下のようなXPath式を使用して非常に簡単にこれを行うことができます:誰かが私のexisitngを書き換えた場合

OpenFileDialog dlg = new OpenFileDialog(); 
    dlg.Title = "Open XML document"; 
    dlg.Filter = "XML Files (*.xml)|*.xml"; 
    dlg.FileName = Application.StartupPath + "\\..\\..\\Sample.xml"; 
    if (dlg.ShowDialog() == DialogResult.OK) 
    { 
     try 
     { 
      //Just a good practice -- change the cursor to a 
      //wait cursor while the nodes populate 
      this.Cursor = Cursors.WaitCursor; 
      //First, we'll load the Xml document 
      XmlDocument xDoc = new XmlDocument(); 
      xDoc.Load(dlg.FileName); 
      //Now, clear out the treeview, 
      //and add the first (root) node 

      treeView1.Nodes.Clear(); 
      TreeNode rootTreeNode = new TreeNode(xDoc.DocumentElement.Name); 
      treeView1.Nodes.Add(rootTreeNode); 

      foreach (XmlNode titleNode in xDoc.DocumentElement.SelectNodes(@"//title")) 
      { 
       rootTreeNode.Nodes.Add(titleNode.InnerText); 
      } 

      treeView1.ExpandAll(); 
     } 
     catch (XmlException xExc) 
     //Exception is thrown is there is an error in the Xml 
     { 
      MessageBox.Show(xExc.Message); 
     } 
     catch (Exception ex) //General exception 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      this.Cursor = Cursors.Default; //Change the cursor back 
     } 
    }} 
+0

働いてくれてありがとう – michale

関連する問題