注:以下举例仅针对xml自定义了命名空间的情况,如果是其他情况,请参照他人博客~

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

private XmlDocument xmlDoc;
        protected void Button1_Click(object sender, EventArgs e)
        {
            LoadXml();

————申明xml命名空间管理对象
            XmlNamespaceManager m = new XmlNamespaceManager(xmlDoc.NameTable);   

————添加命名空间 
            m.AddNamespace("fuck", "http://www.google.com/schemas/sitemap/0.84");       

————注意用法,原本路径是“urlset/url”,须改写为“/fuck:urlset/fuck:url”,依此类推。
            XmlNodeList nodelist = xmlDoc.SelectNodes("/fuck:urlset/fuck:url",m);         

if (nodelist == null)
            {
                Page.RegisterStartupScript("", "alert('列表是空的!')");
            }
            else
            {
                foreach (XmlNode node in nodelist)
                {
                    if (node["priority"].InnerText == "0.4")
                    {
                        node["priority"].InnerText = "0.8";
                    }
                }
                xmlDoc.Save(Server.MapPath("XML/test2.xml"));
            }
        }
        private void LoadXml()
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("XML/test1.xml"));
        }

下面是我的xml文件——

———这个xmlns属性值就是xml自定义的命名空间!上面命名空间的值就是根据这里来写的!

   <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">      
- <url>
   <loc>http://www.baidu.com/</loc>
   <lastmod>2013-12-05</lastmod>
   <changefreq>daily</changefreq>
   <priority>0.5</priority>
   </url>
-  <url>
   <loc>http://www.baidu.com/Default.htm</loc>
   <lastmod>2013-12-05</lastmod>
   <changefreq>weekly</changefreq>
   <priority>0.4</priority>
   </url>
</urlset>

当使用xmldocument.selectnodes()时,如果xml文件中有自定义的命名空间的话(也就是根节点属性值),在使用selectnodes()函数时,一定要记得增加命名空间。

关于这一点,官网上也有说明:

Remarks


XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager.

在此附上官网链接,说的很详细,还有例子。XmlNode.SelectNodes Method (String, XmlNamespaceManager)

最新文章

  1. kobject.c 添加注释
  2. 客户端缓存(Client Cache)
  3. spark streaming - kafka updateStateByKey 统计用户消费金额
  4. 关于jQuery的小知识点
  5. css_样式样式器的分类
  6. 基于Linux2.6内核的加密容器法保护文件方法
  7. 图的最小生成树(Prim、Kruskal)
  8. ArcGIS添加鹰眼
  9. 在SQL 中生成JSON数据
  10. 简单字符串处理 hdu2532 Engine
  11. ionic常用命令记录
  12. Python面试题解答
  13. 前端的UI设计与交互之字体篇
  14. hdu 5514 Frogs(容斥)
  15. vue关于为空使用默认值
  16. linux修改主机名+免密认证+关闭防火墙
  17. CS61B HW0
  18. 解决Android Studio 错误方法
  19. awr format
  20. GCD与莫比乌斯反演的勾当

热门文章

  1. hdoj 2178 猜数字
  2. [iOS基础控件 - 3.5] NSBundle, UIImageView和UIButton的区别, 模拟器和文档
  3. C#操作符的重载
  4. [开心IT面试题] realloc用法
  5. css中文字体unicode对照表
  6. jboss7的服务器开启和关闭命令
  7. 【转】解决HttpServletResponse输出的中文乱码问题
  8. 学习 MFC之 工具栏(二)
  9. Android 百度地图API 定位 导航
  10. nopCommerce的源代码结构和架构