XML文件:<histNews xmlns="http://www.163.com/rss/0.9">
  <news>
    <title>什么什么什么</title>
    <time>2008-12-11</time>
  </news>
<histNews>读取方式:
  string url = Server.MapPath("/sitemap/Sitemaps.xml");
  XmlDocument xmlDoc = new XmlDocument();
  XmlNode root = xmlDoc.SelectSingleNode("histNews");
  XmlElement news = xmlDoc.CreateElement("url");错误提示:System.NullReferenceException: 未将对象引用设置到对象的实例我知道SelectSingleNode(xpath)有问题,但是不知道应该怎么改

解决方案 »

  1.   

    要先加载xml文件:XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load(url);
      

  2.   

    忘记复制上来了,已经xmlDoc.Load(url);了
      

  3.   

      string url = Server.MapPath("/sitemap/Sitemaps.xml"); 
      XmlDocument xmlDoc = new XmlDocument(); 
      xmlDoc.Load(url);//..添加这句
      XmlNode root = xmlDoc.SelectSingleNode("histNews"); 
      XmlElement news = xmlDoc.CreateElement("url"); 
      

  4.   

    忘记复制上来了,已经xmlDoc.Load(url);了
      

  5.   

      string url = Server.MapPath("/sitemap/Sitemaps.xml"); 
      XmlDocument xmlDoc = new XmlDocument(); 
      xmlDoc.Load(url);
      XmlNode root = xmlDoc.SelectSingleNode("histNews"); 
      XmlElement news = xmlDoc.CreateElement("url"); 
      

  6.   

    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load(url);
    你没有加载,xmlDocument的Document对像肯定为null了,
      

  7.   

    忘记复制上来了,已经xmlDoc.Load(url);了
    不是这个问题,救命~~~~~~~~~~~~~~~
      

  8.   


    忘记复制上来了,已经xmlDoc.Load(url);了 
    不是这个问题,救命~~~~~~~~~~~~~~~
      

  9.   

    怎么一样的贴子,XmlDocument xmlDoc = new XmlDocument(); 
    这时候xmlDoc的Document还是空的,所以需要Load
    加上
    xmlDoc.Load(url);
      

  10.   

    忘记复制上来了,已经xmlDoc.Load(url);了
    不是这个问题,救命~~~~~~~~~~~~~~~~~
      

  11.   

    忘记复制上来了,已经xmlDoc.Load(url);了
    不是这个问题,救命~~~~~~~~~~~~~~~~~
      

  12.   

    就搂主的这四句代码,是没问题的,也不会产生System.NullReferenceException异常,虽然在使用xmlDoc对象之前没有load文件我估计是搂主这四句后面的代码有问题。搂主是不是想创建这个XML文件 ?
      

  13.   

    你的xmlDoc.Load()应该抛异常了,看看。
      

  14.   

    <histNews xmlns="http://www.163.com/rss/0.9"> 
      <news> 
        <title>什么什么什么 </title> 
        <time>2008-12-11 </time> 
      </news> 
    </histNews>注意结束标记。 
      

  15.   


    XmlNode root = xmlDoc.DocumentElement;忘了,改过一行
      

  16.   


    XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
                xnm.AddNamespace("bbs", "http://www.163.com/rss/0.9");
                XmlNode root = xmlDoc.SelectSingleNode("bbs:histNews",xnm);
    这样也可以。
      

  17.   


    晕,无语~~~~~~~~~
    <histNews xmlns="http://www.163.com/rss/0.9" >
    大哥,这里我特意加色了,注意一下
    <histNews xmlns="http://www.163.com/rss/0.9">
      <news>
        <title></title>
        <time></time>
      </news>
    </histNews>