NevinsLtd
asked on
C# issues with generating sitemap.xml dynamically
Hello,
I want to generate sitemap.xml dynamically and having trouble reading the existing sitemap.xml and updating it. First step (code show bellow) is to clear the sitemap.xml file node urlset.
when code is executed, I'm getting the following error:
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
selectedNode.RemoveAll();
One thing I noticed is if I clear urlset attributes it will work fine
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
obviously, the problem is that it will not become a valid sitemap..
appreciate your help
I want to generate sitemap.xml dynamically and having trouble reading the existing sitemap.xml and updating it. First step (code show bellow) is to clear the sitemap.xml file node urlset.
when code is executed, I'm getting the following error:
System.NullReferenceExcept
selectedNode.RemoveAll();
One thing I noticed is if I clear urlset attributes it will work fine
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
obviously, the problem is that it will not become a valid sitemap..
appreciate your help
string configPath = Server.MapPath("~/sitemap.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configPath);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
XmlNode selectedNode = xmlDoc.SelectSingleNode("//urlset", nsMgr);
selectedNode.RemoveAll();
XmlElement nodeElmt = xmlDoc.CreateElement("urlset");
xmlDoc.Save(Server.MapPath(configPath));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that solved the first challange but created another challenge which is producing the follownig tag:
<url xmlns="">
which should be <url>