我希望能够在Global.asax中动态的修改Web.sitemap文件,它虽然是XML但是与普通的XML的操作方式还是不一样的。
示例代码如下:
string xmlPath = Server.MapPath("Web.sitemap");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("sm", "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0");
XmlNode root = xmlDoc.SelectSingleNode("//sm:siteMap",nsMgr);
XmlElement xele = xmlDoc.CreateElement("siteMapNode");
XmlElement xelesub1 = xmlDoc.CreateElement("siteMapNode");
说明:
If the XML document defines namespaces then you need to use your XmlNamespaceManager to bind prefixes to those namespaces and then you need to use those prefixes in your XPath expressions and you need to pass the XmlNamespaceManager instance as the second argument to the SelectNodes or SelectSingleNode call