Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. A parent or not a parent, that is the question...

A parent or not a parent, that is the question...

Scheduled Pinned Locked Moved XML / XSL
questioncsharpdatabasexml
3 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nood e
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • N Nood e

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • C Christian Graus

        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

        N Offline
        N Offline
        Nood e
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups