C# issues with generating sitemap.xml dynamically

Posted on 2009-04-24
Last Modified: 2012-05-06

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.NullReferenceException: Object reference not set to an instance of an object.

One thing I noticed is if I clear urlset attributes it will work fine
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();


        XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);

        XmlNode selectedNode = xmlDoc.SelectSingleNode("//urlset", nsMgr);


        XmlElement nodeElmt = xmlDoc.CreateElement("urlset");


Open in new window

Question by:NevinsLtd
    LVL 15

    Accepted Solution

    The XmlNamespaceManager doesn't cope well with default namespaces, ie those without a prefix. Try the following:
    nsMgr.AddNamespace("default", "");
    XmlNode selectedNode = xmlDoc.SelectSingleNode("//default:urlset", nsMgr);

    Open in new window


    Author Comment

    thank you for your quick response..
    that solved the first challange but created another challenge which is producing the follownig tag:
     <url xmlns="">

    which should be  <url>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now