Wpf 与子节点的内部文本进行字符串比较

Wpf 与子节点的内部文本进行字符串比较,wpf,xml,Wpf,Xml,在下面的代码中,我尝试使用streamreader进行文本解析。这是为了从文本文件中获取电子邮件地址。如果我没有电子邮件地址,组合框将留空(索引=-1)。如果我的xml文件中有一封电子邮件,那么我会选择它。否则,我将使用新的电子邮件地址向xml文件中添加一个节点 代码: private void Textparsing() { using (StreamReader sr = new StreamReader(Masterbuildpropert

在下面的代码中,我尝试使用streamreader进行文本解析。这是为了从文本文件中获取电子邮件地址。如果我没有电子邮件地址,组合框将留空(索引=-1)。如果我的xml文件中有一封电子邮件,那么我会选择它。否则,我将使用新的电子邮件地址向xml文件中添加一个节点

代码:

private void Textparsing()
    {               
      using (StreamReader sr = new StreamReader(Masterbuildpropertiespath))                 
            {
                while (sr.Peek() >= 0)
                if (line.StartsWith("Builder_Email:"))
                    {   
                        string[] fields = line.Split('\t');
                        string builderemail = fields[3];
                        XmlDocument emailparse = new XmlDocument();
                        emailparse.LoadXml(@"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)
                                {
                                    int count = emailparse.SelectNodes("email/builderemail/builder").Count;
                                    count--;
                                    comboBox1.SelectedIndex = count;
                                }
                                else
                                {
                                    //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;
                                }
                            }

          }
但是,我在这一行得到一个XmlException(根级别的数据无效。第1行,位置1。):

emailparse.LoadXml(@"C:\GUI\buildermanageremail.xml");
为什么会这样

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<email>
  <builderemail>
    <builder>
      <value>abc@123.com</value>
    </builder>
    <builder>
      <value>Others</value>
    </builder>
  </builderemail>
  <manageremail>
    <manager>
      <value>abc@456.com</value>
    </manager>
    <manager>
      <value>Others</value>
    </manager>
  </manageremail>
</email>

abc@123.com
其他
abc@456.com
其他
您应该使用

       emailparse.Load(@"C:\GUI\buildermanageremail.xml");
方法代替

      emailparse.LoadXml(@"C:\GUI\buildermanageremail.xml");
因为LoadXml可以加载xml字符串,而不是文件