Wpf 如何选择节点的索引?

Wpf 如何选择节点的索引?,wpf,combobox,Wpf,Combobox,我有一个xml填充的组合框。如果使用streamreader解析文本生成的builderemail等于xml文件中的任意一个值,则组合框将选择索引。我如何选择它 if (line.StartsWith("Builder_Email:")) { bool IsNodeExists = false; string[] field

我有一个xml填充的组合框。如果使用streamreader解析文本生成的builderemail等于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;
                                }
                            }

我相信这段代码做了你想让你的代码做的一切。这段代码当然不是完美的,甚至可能不起作用,但如果您将它与您的代码进行比较,您会发现它使用了可能六种您在代码中没有遵循并且应该遵循的实践。如果删除所有断言,您会发现只有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下有一个单独的值元素,而不是只包含值的builderEmail,这有什么原因吗

正确的索引是什么意思?什么是someInteger someInteger指的是一个值,无论是节点计数还是其他什么。最后,你能用非常简单的术语重新表述你的问题吗?我不明白正确的索引是什么意思,可能你已经得到了正确的索引。