A parent or not a parent, that is the question...
-
I was wondering if anyone has found an elegant solution to find if an xml node (of any XmlNodeType) is a child of another xml node using .NET? Lol! - and I can't just use (node.Parent == parent) because it may not be a direct desendant :( The code I came up with is: private bool IsChild(XmlNode parent, XmlNode child, bool directDescendantsOnly) { return ((child.ParentNode == parent)? (true): (directDescendantsOnly? (false): (((child.ParentNode == child.OwnerDocument)? (false): IsChild(parent, child.ParentNode, directDescendantsOnly))) )); } But it fails when you query NameSpace nodes, etc. It seems to be getting more and more special case, which is annoying...so please, if anyone has already tackled this one, can you let me know? Many thankyous, Ben
-
I was wondering if anyone has found an elegant solution to find if an xml node (of any XmlNodeType) is a child of another xml node using .NET? Lol! - and I can't just use (node.Parent == parent) because it may not be a direct desendant :( The code I came up with is: private bool IsChild(XmlNode parent, XmlNode child, bool directDescendantsOnly) { return ((child.ParentNode == parent)? (true): (directDescendantsOnly? (false): (((child.ParentNode == child.OwnerDocument)? (false): IsChild(parent, child.ParentNode, directDescendantsOnly))) )); } But it fails when you query NameSpace nodes, etc. It seems to be getting more and more special case, which is annoying...so please, if anyone has already tackled this one, can you let me know? Many thankyous, Ben
Would recursing though the children work better ( although it would take longer ) ? Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
Would recursing though the children work better ( although it would take longer ) ? Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
hm - it might work (it should in theroy), but this code is quite core to the project I'm doing, so it's worth getting it to run as fast as possible. Admittedly getting it to run in the first place is always a good plan, but I think working backwards from the child, rather than forwards from the suggested parent, is a better plan. Thanks for your help anyway! Ben