与子节点的innertext进行字符串比较

时间:2011-03-21 06:47:44

标签: wpf xml

在下面的代码中,我尝试使用streamreader进行文本解析。这是从文本文件中获取电子邮件地址。如果我没有电子邮件地址,则组合框保留为空(index = -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");

为什么会这样?

我的xmlfile:

<?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>

1 个答案:

答案 0 :(得分:1)

你应该使用

       emailparse.Load(@"C:\GUI\buildermanageremail.xml");

方法而不是

      emailparse.LoadXml(@"C:\GUI\buildermanageremail.xml");

因为LoadXml可以加载xml字符串,而不是文件。