关于xml的问题

wujx0104 2006-02-08 11:25:47
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:未将对象引用设置到对象的实例”
请问这是什么原因,我找了好久没发现,请高手指教
...全文
142 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wujx0104 2006-02-08
  • 打赏
  • 举报
回复
我弄了,好像没反应:(在浏览器下面提示javascript错误
startray 2006-02-08
  • 打赏
  • 举报
回复

点击上面的管理按钮

回复 | 收藏此页 | 专题 | 公告 | 管理 | 保存 | 关闭窗口
wujx0104 2006-02-08
  • 打赏
  • 举报
回复
再次感谢。怎么给你分啊?
startray 2006-02-08
  • 打赏
  • 举报
回复
//表示从根目录开始
wujx0104 2006-02-08
  • 打赏
  • 举报
回复
谢谢,解决了。为什么要加"//"呢?
startray 2006-02-08
  • 打赏
  • 举报
回复
XmlNode root=xmlDoc.SelectSingleNode("//channel");//
wujx0104 2006-02-08
  • 打赏
  • 举报
回复
<?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帮我看看
startray 2006-02-08
  • 打赏
  • 举报
回复
channel可能不是根节点的
返回来的root节点为null
所以提示出错
检查你的xml文件
贴出来:
//channel

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧