开发者

Find node by title and delete it, but always is deleted first node - asp.net

in my Web.sitemap I have this:

<?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>

I call DeleteNode("Něco");

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;
}

And I dont get any errors, so it is OK, but in sitemap isnt deleted node with title="Něco" but always first node, so in sitem开发者_开发百科ap is this:

<?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>

Do u know why?


This compiles just fine. Also when running the code it deletes the <siteMapNode url="Neco" title="Něco" /> line from the file like it is supposed to.

Can't see your problem ;-)

For copy n' paste pleasure:

using System;
using System.Xml;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DeleteNode("Něco");
        }

        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("C:\\test.xml");
            return doc;
        }

        private static void SaveXmlDoc(XmlDocument doc)
        {
            string AbsPath = "C:\\test.xml";
            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;
        }
    }
}

Also when setting a breakpoint at node.ParentNode.RemoveChild(node); I can see that the Neco node is currectly found - well of course, because it was readily deleted.

You know what I think? Since you are running the code on a webserver: Check your encoding! can't stress that enough. Your Neco probably is returned wrongly, make sure you use UTF-8 everywhere (http headers, the file, no BOM in the .sitemap file).


Does this compile? This line does not look like it would.

XmlElement node = doc.SelectSingleNode(xPath) as XmlElement;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜