我有這樣的:查找節點的標題,並刪除它,但總是被刪除第一個節點 - 在我Web.sitemap中asp.net
<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode url="~/" title="Úvodní stránka">
<siteMapNode url="Pocitace" title="Počítače" />
<siteMapNode url="Elektronika" title="Elektronika" />
<siteMapNode url="Neco" title="Něco" />
</siteMapNode>
</siteMap>
我叫DeleteNode(「尼哥」);
public static void DeleteNode(string title)
{
XmlDocument doc = LoadXmlDoc();
XmlElement node = FindNodeByTitle(doc, title);
node.ParentNode.RemoveChild(node);
SaveXmlDoc(doc);
}
private static XmlDocument LoadXmlDoc()
{
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("../../Web.sitemap"));
return doc;
}
private static void SaveXmlDoc(XmlDocument doc)
{
string AbsPath = HttpContext.Current.Server.MapPath("../../Web.sitemap");
doc.Save(AbsPath);
}
private static XmlElement FindNodeByTitle(XmlDocument doc, string title)
{
string xPath = String.Format("//*[@title='{0}']", title);
XmlElement node = doc.SelectSingleNode(xPath) as XmlElement;
if(node == null)
throw new Exception("Node not found with title: " + title);
return node;
}
而且我沒有得到任何錯誤,所以它是確定的,但在站點地圖的心不是刪除節點與標題=「尼哥」,但總是第一個節點,所以在網站地圖是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode url="~/" title="Úvodní stránka">
<siteMapNode url="Elektronika" title="Elektronika" />
<siteMapNode url="Neco" title="Něco" />
</siteMapNode>
</siteMap>
待辦事項你知道爲什麼嗎?
會顯示如何使用Linq做到這一點的幫助? – Sorax 2011-03-31 21:10:27