private static XmlNode GetRoot(XmlNode node, ref int depth)
{
depth = 0;
XmlNode curNode = node;
XmlNode parent = ((curNode.NodeType == XmlNodeType.Attribute) ? (((XmlAttribute)curNode).OwnerElement) : (curNode.ParentNode));
for (; parent != null; depth++)
{
curNode = parent;
parent = curNode.ParentNode; // no need to check for attribute since navigator can't be built on its children or navigate to its children
}
return curNode;
}