2012-07-26 85 views
0

这是我的xml文件;如何将xml数据绑定到comboBox以制作智能?

<UserClass> 
    <Id>1</Id> 
    <Name>oss</Name> 
    <Address> 
    <Id>1</Id> 
    <Street>asstreet</Street> 
    </Address> 
</UserClass> 

所以我想将这些“节点”添加到组合框项目。 当用户输入UserClass并输入“。”(点)到“UserClass”的末尾时; ID,名称和其他东西必须在组合框中列出。

用户键入“UserClass”。和 - > combobox得到这些;

UserClass.Id 
UserClass.Name 
UserClass.Address.Id 
UserClass.Address.Street 

我尝试了很多东西,包括那一个;

... 
    try 
    { 
     string parsedNode = ParseComboBox(); 
     XmlReader rdr = XmlReader.Create(new System.IO.StringReader(_globalXml)); 

     comboBox1.Items.Clear(); 
     while (rdr.Read()) 
     { 
      if (rdr.NodeType == XmlNodeType.Element) 
      { 
       comboBox1.Items.Add(rdr.LocalName); 
      } 

      comboBox1.DroppedDown = true; 
     } 



     //string parsedNode = ParseComboBox(); 
     //XmlNodeList childList = xml.GetElementsByTagName(parsedNode); 

     ////comboBox1.Items.Clear(); 
     //foreach (XmlNode node in childList) 
     //{ 
     // foreach (var osman in node.ChildNodes) 
     // { 
     //  comboBox1.Items.Add(parsedNode + "." + osman); 
     // } 
     //} 


    } 
    catch (Exception) 
    { 
     MessageBox.Show("fuuu"); 
    } 
}... 

    private string ParseComboBox() 
    { 
     string resultAsXmlNodes = null; 
     string text = comboBox1.Text; 

     if (text.EndsWith(".")) 
     { 
      char[] delimiterChars = { '.' }; 

      string[] words = text.Split(delimiterChars); 

      foreach (string s in words) 
      { 
       resultAsXmlNodes += s; 
      } 
     } 

     return resultAsXmlNodes; 
    } 

它工作不正常。我相信有一个简单的方法来做到这一点。 那么,简单的方法是什么?或者干脆, 如何在comboBox中显示节点名称?

+0

快速浏览一下,您的XML解析看起来是不正确的。如果你将责任分离了一点,并且创建一个函数来从给定节点返回所需的字符串,也许调试起来会更容易?一旦你有这个列表,将它绑定到组合框将会简单得多 – Origin 2012-07-26 15:47:57

回答

1

我发现了这个问题。以下是我使用XML文件和一个ComboBox控件为表单项目工作的一些示例代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Xml; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      comboBox1.KeyDown += comboBox1_KeyDown; 
     } 

     private void comboBox1_KeyDown(object sender, KeyEventArgs e) 
     { 
      switch (e.KeyCode) 
      { 
       case Keys.Decimal: 
       case Keys.OemPeriod: 
        LoadComboItems(comboBox1.Text); 
        break; 
       default: 
        break; 
      } 
     } 

     void LoadComboItems(string userInput) 
     { 
      comboBox1.Items.Clear(); 
      string lookupName = userInput.Trim(); 
      if (lookupName.Length > 0) 
      { 
       string _globalXML = Application.StartupPath + @"\XMLFile1.xml"; 
       XmlReader rdr = XmlReader.Create(_globalXML); 

       while (rdr.Read()) 
       { 
        if (rdr.LocalName == lookupName) 
        { 
         string ElementName = ""; 
         int eCount = 0; 
         int prevDepth = 0; 
         while (rdr.Read()) 
         { 
          if (rdr.NodeType == XmlNodeType.Element) 
          { 
           ElementName += '.' + rdr.LocalName; 
           eCount++; 
          } 
          else if (rdr.NodeType == XmlNodeType.EndElement && eCount == rdr.Depth) 
          { 
           if (rdr.Depth >= prevDepth) 
           { 
            comboBox1.Items.Add(lookupName + ElementName); 
            int pos = ElementName.LastIndexOf('.'); 
            ElementName = ElementName.Substring(0, pos); 
            prevDepth = rdr.Depth; 
           } 
           eCount--; 
          } 
         } 

        } 
       } 

       if (rdr != null) 
       { 
        rdr.Close(); 
       } 

       comboBox1.DroppedDown = true; 
      } 
     } 
    } 
} 
+0

非常感谢你的盟友。你的答案是一个解决方案,它有小错误(当用户,但光标选择移动到第一个字符),但它的工作。但这对于这份工作来说代码太多了。我试图用最佳实践来解决它。如果您添加了书签或观看此主题,则会看到我的代码(当我正确解决问题时)。再一次非常感谢你。 – 2012-07-27 05:07:19