XmlDocument xmlDoc=new XmlDocument(); 
xmlDoc.Load("book.xml");
XmlNode root=xmlDoc.SelectSingleNode("channel");//查找<channel> 
XmlElement xe1=xmlDoc.CreateElement("item");//创建一个<item>节点 XmlElement xesub1=xmlDoc.CreateElement("title"); 
xesub1.InnerText=txt_title.Text;//设置文本节点 
xe1.AppendChild(xesub1);
root.AppendChild(xe1);提示我“System.NullReferenceException:未将对象引用设置到对象的实例”
请问这是什么原因,我找了好久没发现,请高手指教

解决方案 »

  1.   

    channel可能不是根节点的
    返回来的root节点为null
    所以提示出错
    检查你的xml文件
    贴出来:
    //channel
      

  2.   

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
      <channel>
        <title>新闻中心-新闻要闻</title>
        <image>
          <title>新闻中心</title>
          <link>http://news.sina.com.cn</link>
          <url>http://image2.sina.com.cn/dy/sina_news626.gif</url>
        </image>
        <description>新闻中心-新闻要闻</description>
        <link>http://news.sina.com.cn/iframe/o/allnews/input/index.htm</link>
        <language>zh-cn</language>
        <generator>WWW.SINA.COM.CN</generator>
        <ttl>5</ttl>
        <copyright>Copyright 1996 - 2005 SINA Inc. All Rights Reserved</copyright>
        <pubDate>Tue, 7 Feb 2006 07:50:03 GMT</pubDate>
        <category>
        </category>
        <item>
          <title>戴秉国9日将率团赴日本参加第四轮中日对话</title>
          <link>http://news.sina.com.cn/c/2006-02-07/15288145311s.shtml</link>
          <author>WWW.SINA.COM.CN</author>
          <guid>http://news.sina.com.cn/c/2006-02-07/15288145311s.shtml</guid>
          <category>
          </category>
          <pubDate>Tue, 7 Feb 2006 07:28:24 GMT</pubDate>
          <comments>
          </comments>
          <description>  新华网快讯:外交部副部长戴秉国将于2月9日率团赴日本,与日方举行第四轮中日战略对话。 
      中日历次战略对话
      2005年10月14日:第三轮中日战略对话举行 戴秉国率团出席
      2005年6月23日:中国日本举行第二次战略对话谈当前中日关系
      2005年05月13日:中日首次战....</description>
        </item>
      </channel>
      </rss>
    就是这个xml帮我看看
      

  3.   

    XmlNode root=xmlDoc.SelectSingleNode("//channel");//
      

  4.   

    我弄了,好像没反应:(在浏览器下面提示javascript错误