日期:2014-05-18  浏览次数:20866 次

请问各位专家关于读取XML NODE之值的问题
各位专家我想请教一下,我在程序中建立一个button当他按下时,会将XML中这段<OnList>abc@gmail.com</OnList>的

值"abc@gmail.com"指派给label.text,但是在执行出现 "并未将对象参考设定为对象的实例" 的例外讯息,当我下断点时,

发现 label1.Text = null ,xdMailList 的 DocumentElement底下InnerText之值为abc@gmail.com与InnerXml 之值为

<OnList>abc@gmail.com</OnList> ,请问一下如何处理。

MailList.xml
XML code

<?xml version="1.0" encoding="utf-8" ?>

<MailList>
    <OnList>abc@gmail.com</OnList>
</MailList>



C# code

private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xdMailList = new XmlDocument();
            xdMailList.Load(@"MailList.xml");

            // textBox_ServerName.Text = document.SelectSingleNode("/travianBOT/settinginfo").Attributes["server"].Value;

            label1.Text = xdMailList.SelectSingleNode("MailList").Attributes["OnList"].Value.ToString();
        }








------解决方案--------------------
label1.Text = xdMailList.SelectSingleNode("/MailList/OnList").InnerText;
------解决方案--------------------
C# code
private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xdMailList = new XmlDocument();
            xdMailList.Load(@"MailList.xml");

            // textBox_ServerName.Text = document.SelectSingleNode("/travianBOT/settinginfo").Attributes["server"].Value;

            label1.Text = xdMailList.SelectSingleNode("/MailList/OnList").InnerText;
        }

------解决方案--------------------
Attributes是属性~~~~~