Monday, 23 July 2007

How do i find the xpath of an XmlNode in c#?

I came across this problem today where I was trying to find the xpath of the current node, so i could access the parent and remove that node from the collection. I knocked together some useful code to remove this. It basically works out the position of that node in the document and then which child it is on the child node.

/// <summary>
/// Gets an xpath to a node
/// </summary>
/// <param name="node">Node to get xpath for</param>
/// <returns>Xpath for node</returns>
private static string GetXPathToNode(XmlNode node)
if (node.NodeType == XmlNodeType.Attribute)
// attributes have an OwnerElement, not a ParentNode; also they have
// to be matched by name, not found by position
return String.Format(
if (node.ParentNode == null)
//Have root node - so return empty path
return "";

// the path to a node is the path to its parent, plus "/node()[n]", where
// n is its position among its siblings.
return String.Format(

/// <summary>
/// Gets node position in relation to the child
/// </summary>
/// <param name="child">Child</param>
/// <returns>Position on parent</returns>
private static int GetNodePosition(XmlNode childNode)
for (int position = 0; position < childNode.ParentNode.ChildNodes.Count; position++)
if (childNode.ParentNode.ChildNodes[position] == childNode)
// need to add one as xpath index starts not at zero
return position + 1;
throw new InvalidOperationException("Missing Child Node");

Thursday, 12 July 2007

JavaScript popup window features

Here are the options for the JavaScript method, just set =1 if you want them included or =0 if you want them hidden.

Determines if you want the status bar at the bottom of the window to be shown

This shows/hides the standard browser bar i.e. the back and forward buttons amongst others.

The location bar where you enter url’s.

This shows or hides the menu bar for the browser.

This shows or hides the directories toolbar – i.e. whats new etc.

Enables/disables the user to resize the browser window.

Enable the scrollbars if the document takes up more area than the window

The height of the window in pixels. (i.e.: height='550')

The width of the window in pixels.