0
在下面的代碼中,我試圖通過使用一個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>[email protected]</value>
</builder>
<builder>
<value>Others</value>
</builder>
</builderemail>
<manageremail>
<manager>
<value>[email protected]</value>
</manager>
<manager>
<value>Others</value>
</manager>
</manageremail>
</email>
謝謝,錯過了這部分 – jeremychan 2011-03-21 07:57:05