2011-03-21 138 views
0

我有一個xml填充組合框。如果builderemail(使用streamreader解析文本)等於xml文件中找到的任何一個值,組合框將選擇索引。我如何選擇它?我如何選擇節點的索引?

if (line.StartsWith("Builder_Email:")) 
         { 
          bool IsNodeExists = false; 
          string[] fields = line.Split('\t'); 
          string builderemail = fields[3]; 
          XmlDocument emailparse = new XmlDocument(); 
          emailparse.Load(@"C:\GUI\buildermanageremail.xml"); 
          XmlNodeList emailnode = emailparse.GetElementsByTagName("value"); 
          if (string.IsNullOrEmpty(builderemail)) 
           comboBox1.SelectedIndex = -1; 
          else 
          foreach (XmlNode node in emailnode) 
          { 
           if (builderemail == node.InnerText) 
           { 
           // how do i get the combobox selection right? 
           // need some code here 
            IsNodeExists = true; 
            break; 
           } 
          } 
          if(!IsNodeExists) 
          { 
           //create main node 
           XmlNode abc = emailparse.CreateNode(XmlNodeType.Element, "builder", null); 

           //create the first child node 
           XmlNode value = emailparse.CreateElement("value"); 
           //set the value 
           value.InnerText = builderemail; 

           // add childes to father 
           //node.AppendChild(id); 
           abc.AppendChild(value); 

           // find the node we want to add the new node to 
           XmlNodeList l = emailparse.GetElementsByTagName("builderemail"); 
           // append the new node 
           l[0].AppendChild(abc); 
           // save the file 
           emailparse.Save(@"C:\GUI\buildermanageremail.xml"); 

           //then we populate the new updated xml file into the drop down list: 
           PopulateDDLFromXMLFile(); 

           int count = emailparse.SelectNodes("email/builderemail/builder").Count; 
           count--; 
           comboBox1.SelectedIndex = count; 
          } 
         } 

看的地方是在這裏:

foreach (XmlNode node in emailnode) 
          { 
           if (builderemail == node.InnerText) 
           { 
           // how do i get the combobox selection right? 
           // need some code here 
            IsNodeExists = true; 
            break; 
           } 
          } 
+0

你說的'正確index'意思,什麼是'someinteger' – 2011-03-21 07:54:17

+0

someinteger指的是價值,無論是如果一個節點計數或東西 – jeremychan 2011-03-21 07:56:08

+0

你能不能改一下你的問題到底在簡單來說,我不能」不明白什麼**正確的索引**意味着,可能你已經獲得了正確的索引。 – 2011-03-21 07:57:15

回答

0

我相信這個代碼,你希望你的代碼做的一切。這段代碼當然不是完美的,甚至可能不起作用,但如果將它與您的代碼進行比較,您應該發現它大概採用了六種實踐方法,而您在代碼中沒有遵循這些規範並且可能應該這樣做。如果你刪除了所有的斷言,你會發現它只有10行代碼(不包括重構的方法)。

if (line.StartsWith("Builder_email:")) 
{ 
    Debug.Assert(
     line.Where(x => x == '\t').Count() > 2), 
     "Can't parse input line."); 
    string builderEmail = line.Split('\t')[3]; 
    Debug.Assert(
     builderEmail != null && builderEmail == builderEmail.Trim(), 
     "Input data is bad."); 
    string filename = @"C:\GUI\buildermanageremail.xml" 
    Debug.Assert(
     File.Exists(filename), 
     "Expected XML file does not exist."); 
    XmlDocument emailXml = new XmlDocument(); 
    emailXml.Load(filename); 

    // In your real application, you know the name of the document element, so you 
    // should replace * with it in the XPath below. 
    string xpath = string.Format(
     "/*/builderemail/builder/value[.='{0}']", 
     builderEmail); 
    if (emailXml.SelectSingleNode(xpath) == null) 
    { 
     CreateBuilderEmailElement(emailXml, builderEmail); 
     emailXml.Save(filename); 
     // I've changed the name of this method, which is problematic for several 
     // reasons - not least of which is that in my world, at least, "DDL" means 
     // "Data Definition Language." 
     // 
     // This also assumes that you've created an overload of the method that 
     // takes an XmlDocument argument. 
     PopulateEmailComboBox(emailXml); 
    } 
    // I'm assuming that the builderEmail is the actual text value stored in the 
    // combo box items, in which case all you need to do is find the item with that 
    // value and set SelectedItem, which will automatically set SelectedIndex. Also, 
    // if the value isn't found, setting SelectedItem to null automatically sets 
    // SelectedIndex to -1. 
    builderEmailComboBox.SelectedItem = builderEmailComboBox.Items 
     .Where(x => x.ToString() == builderEmail) 
     .FirstOrNull(); 
} 

下面是用於創建builderemail元件的方法 - 這,順便說一下,應該命名爲builderEmail,如果您有任何說了吧:

// this is refactored out as its own function, and made internal so that you 
// can unit test it. 
internal void CreateBuilderEmailElement(XmlDocument emailXml, string builderEmail) 
{ 
     XmlElement builder = emailXml.CreateNode("builder"); 
     XmlElement value = emailXml.CreateNode("value"); 
     builder.AppendChild(valueElm); 
     value.InnerText = builderEmail; 
     // again, you know the name of the document element in your application, 
     // so replace the * below with it. 
     Debug.Assert(
     emailXml.SelectSingleNode("/*/builderemail") != null, 
     "No builderemail element found under the document element."); 
     emailXml.SelectSingleNode("/*/builderemail").AppendChild(builder); 
} 

此外,有一個原因,你的XML在builderEmail下有一個單獨的value元素,而不是builderEmail只包含值?