• 0

C#: Getting an XPath expression from XmlDocument ?


Question

Hello,

Is there an easy way of getting an XPath expression to where you are in an XML document ? I am parsing an XMLDocument using an XmlTextReader. When I reach a new element, I`d like to be able to compute the XPath to that element as something like:

article[1]/section[2]/p[6]

Does anyone know of a simple solution ?

.:BoeManE:.

5 answers to this question

Recommended Posts

  • 0

You'll have to implement your own tracking code. XmlReader is just a forward reading XML scanner, that also checks for correctness of the XML file, but nothing more. There are derived classes that perform validation too, but that's about it.

  • 0
  Tom Servo said:
You'll have to implement your own tracking code. XmlReader is just a forward reading XML scanner, that also checks for correctness of the XML file, but nothing more. There are derived classes that perform validation too, but that's about it.

I was afraid of that. :cry:

Thanks for your help though!

.:BoeManE:

  • 0

Check this out (a link that just might help). It helped me with some stuff I was doing and it comes straight from a microsoft dude. Some of the code didn't work for me in C#. It's cool thought cause it'll give you the path from any given node. However, I think you have to have an XmlDocument loaded and not an XmlReader.

  • 0
  MrRogers said:
Check this out (a link that just might help). It helped me with some stuff I was doing and it comes straight from a microsoft dude. Some of the code didn't work for me in C#. It's cool thought cause it'll give you the path from any given node. However, I think you have to have an XmlDocument loaded and not an XmlReader.

Thank you very much. I will definately have a closer look at this!

.:BoeManE:.

  • 0
  boemane said:
Thank you very much. I will definately have a closer look at this!

.:BoeManE:.

Hey.

I have now fixed the problem. I managed to reduce the code calculate the xpath expression to only 6 lines!

If anyone is interested, drop me a line joachim (at) devsiden (dot) info

.:BoeManE:.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Both a game adaptation and it's on Amazon I'll set my hopes low
    • You shut your mouth you young fart, that was just a few years ago, lol.
    • Good effort, but unfortunately not very useful in the real world, since it doesn't support UEFI - which is a major drawback. More than that, most PCs these days have Secure Boot enabled by default, so unless you're tech savvy, the average joe is not going to disable Secure Boot - which means automated Windows > Linux migration programs like this will not work. And if you're tech savvy enough to know how to disable Secure Boot, then you're tech savvy enough to just flash a USB drive and install Linux the usual way - after all, the actual installation process is just point-and-click. So I don't see a use case here to be honest, especially considering the lack of UEFI support - especially when you consider that more and more Linux distros are completely dropping BIOS support (eg Fedora dropped it back in 2022). So this program is a bit too late to the party.
    • I agree with open rights group that the age check companies should be regulated but action does need to be taken imo, they may be called irresponsible parents but the fact is that many don't know how to set up blocks. Why should kids suffer because their parents don't know something? I think the UK has always been a bit like this. We don't say healthcare is your responsibility, we tax everyone and make people register with the NHS. We also don't expect everyone to brush their teeth to maintain dental health, we just put fluoride in their water assuming people aren't brushing enough, because some probably aren't or aren't doing it properly. In general, this way of doing things works.
    • Well that's the weird thing, the UK really isn't a very religious country at all and the same politicians that are backing this (basically all of them) also support things like assisted dying which various religions spoke out against. I don't think this is coming from religion, of course religious groups will support it but I don't think they've started it.
  • Recent Achievements

    • One Month Later
      Falcon.ai earned a badge
      One Month Later
    • Week One Done
      Falcon.ai earned a badge
      Week One Done
    • Dedicated
      EYEREX earned a badge
      Dedicated
    • First Post
      Electronic Person earned a badge
      First Post
    • Week One Done
      CyberCeps666 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      628
    2. 2
      ATLien_0
      236
    3. 3
      Xenon
      164
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!