【问题标题】:delete a node from sitemap xml从站点地图 xml 中删除一个节点
【发布时间】:2015-03-05 15:06:01
【问题描述】:

我的站点地图格式如下。 我想删除一个完整的节点 我找到 loc。 例如:

其中一个节点有<loc>,其值为http://www.my.com/en/flight1。 我想删除<url> 节点和他的孩子 我要删除loclastmodpriority 和比changefreq

<url>
<loc>http://www.my.com/en/flight1
</loc>
<lastmod>2015-03-05</lastmod>
<priority>0.5</priority>
<changefreq>never</changefreq>
</url>


<url>
<loc>
http://www.my.com/en/flight2
</loc>
<lastmod>2015-03-05</lastmod>
<priority>0.5</priority>
<changefreq>never</changefreq>
</url>


<url>
<loc>
http://www.my.com/en/flight3
</loc>
<lastmod>2015-03-05</lastmod>
<priority>0.5</priority>
<changefreq>never</changefreq>
</url>

【问题讨论】:

    标签: c# mvcsitemapprovider xml-sitemap


    【解决方案1】:

    如果你使用 C#,你应该使用 System.xml.linq (XDocument)

    你可以像这样删除一个节点:

    XDocument.Load(/*URI*/);
    
    var elements = document.Root.Elements().Where(e => e.Element("loc") != null && e.Element("loc").Value == "http://www.my.com/en/flight1");
    foreach (var url in elements)
    {
        url.Remove();
    }
    

    【讨论】:

    • 这给出错误:XDocument doc = XDocument.Parse(System.Configuration.ConfigurationManager.AppSettings["SiteMapXMLPath"]); @cedric
    • SiteMapXM‌​LPath 是此文件的路径:yazilimsozluk.com/sitemap.xml 它给出了错误:根级别的数据无效。第 1 行,位置 1。使用此行:XDocument doc = XDocument.Parse(System.Configuration.ConfigurationManager.AppSettings["SiteMapXMLPath"]);
    • 改用 XDocument.Load(uri)
    • 这是 url 变量 sitemaps.org/schemas/sitemap/0.9"> my.com/tr</loc> 2014-02-020.5never 但 url.Element("loc") 为空 i.hizliresim.com/22PW9d.png
    猜你喜欢
    • 2023-03-21
    • 2012-04-19
    • 1970-01-01
    • 2012-09-13
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多