Game Development Reference
In-Depth Information
static int GetNodePosition(XmlNode child)
{
int count = 1;
for (int i = 0; i < child.ParentNode.ChildNodes.Count; i++)
{
if (child.ParentNode.ChildNodes[i] == child)
{
// XPath starts counting at 1, not 0
return count;
}
if (child.ParentNode.ChildNodes[i].Name == child.Name)
{
++count;
}
}
// child node not found in its parent
s ChildNodes property.
throw new InvalidOperationException();
'
}
public 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(
,
GetXPathToNode(((XmlAttribute)node).OwnerElement),
{0}/@{1}
*
//node.Name
);
}
if (node.ParentNode == null)
{
// the only node with no parent is the root node, which has no path
return
“”
;
}
// the path to a node is the path to its parent, plus
/*[n]
, where
// n is its position among its siblings.
return String.Format(
,
GetXPathToNode(node.ParentNode),
{0}/{1}[{2}]
,
GetNodePosition(node)
*
Search WWH ::




Custom Search