Jeremy
Jeremy

Reputation: 169

XmlDocument.SelectSingleNode not always working accurately

I am having an interesting issue with C# and selecting xml nodes consistently.

Our site is in the process of being upgraded from classic ASP to ASP.NET. In order to make the process smoother for visitors while we upgrade, I implemented a 404 redirect to take visitors to the new .aspx that replaced old .asp pages for the case of external links, bookmarks, etc.

Some of the links just changed from .asp to .aspx, but some changed structure and naming, so I have an XML file which follows this pattern:

<?xml version="1.0" encoding="utf-8" ?>
<links>
    <link oldUrl="/jailpod/default.asp" newUrl="/about/construction/jailpod.aspx" />
    <link oldURL="/boc/boards.asp" newUrl="/boc/boards/default.aspx" />
    <link oldUrl="/maps.asp" newUrl="/maps/default.aspx" />
</links>

This XML file is used by our custom 404 page in the C# code behind. The code goes through the process of looking for a replacement URL by searching for a new one in the XML file, seeing if the page simply changed to .aspx, and finally if no match was found staying on the 404 page to let the visitor know they have an incorrect page.

Here is the C# code:

    // Whether or not to display an additional message to the users
    bool redirectVisitor = false;

    string url = ExtractUrlFrom(Request.Url.ToString());

    var doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.Load(Server.MapPath("~/inc/NewUrlDictionary.xml"));

    XmlNode urlNode = doc.SelectSingleNode(
        string.Format("/links/link[@oldUrl='{0}']", url));

            // Check if the node existed or not
            if (urlNode != null)
            {
                url = urlNode.Attributes["newUrl"].Value;
                redirectVisitor = true;
            }
            else
            {
               ....
            }
        }

Here is the issue.

When I visit:

mysite/jailpod/default.asp

OR

mysite/maps.asp

I am redirected as I would expect.

When I visit:

mysite/boc/boards.asp

No XmlNode is found, and thus the 404 page just tells me it does not exist. I have stepped through the C# code using a break point and the URL it is searching for is exactly "/boc/boards.asp" and I even used the Visual Studio locals panel to look through the contents of the XmlDocument object, and it does indeed have a node with the attribute"/boc/boards.asp"

This is not an isolated issue with that one node, there are (seemingly random) other nodes where it occurs as well.

Does anyone have any idea what I am doing wrong here? I am considering just migrating all of these XML records into our database and just querying the database instead -- unless I am just making a silly mistake somewhere here.

Upvotes: 0

Views: 513

Answers (2)

javram
javram

Reputation: 2645

Take a look at your source XML document, the attribute for oldUrl for the one you are having a problem with has a different case than the other entries. xpath is case sensitive.

Upvotes: 2

Luaan
Luaan

Reputation: 63722

Xml attributes are case sensitive. You've got oldURL instead of oldUrl in your second xml element.

Upvotes: 4

Related Questions