1

I have sitemap format as below. I want to delete a complete node that I find loc. For example:

Where a node has <loc>with a value of http://www.my.com/en/flight1. I want to delete the <url> node and his child I want to delete loc than lastmod than priority and than 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>
John Conde
  • 217,595
  • 99
  • 455
  • 496
user1688401
  • 1,851
  • 8
  • 47
  • 83

1 Answers1

1

If you're using C# you should use System.xml.linq (XDocument)

You can remove a node like so:

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();
}
C1rdec
  • 1,647
  • 1
  • 21
  • 46
  • this give error: XDocument doc = XDocument.Parse(System.Configuration.ConfigurationManager.AppSettings["SiteMapXMLPath"]); @cedric – user1688401 Mar 05 '15 at 15:45
  • SiteMapXM‌​LPath is path of this file : http://yazilimsozluk.com/sitemap.xml it give that error: Data at the root level is invalid. Line 1, position 1. with this line: XDocument doc = XDocument.Parse(System.Configuration.ConfigurationManager.AppSettings["SiteMapXMLPath"]); – user1688401 Mar 05 '15 at 15:52
  • Use XDocument.Load(uri) instead – C1rdec Mar 05 '15 at 15:53
  • this is url variable http://www.my.com/tr 2014-02-02 0.5 never but url.Element("loc") is null http://i.hizliresim.com/22PW9d.png – user1688401 Mar 05 '15 at 16:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72354/discussion-between-cedric-and-user1688401). – C1rdec Mar 05 '15 at 16:20