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. Need help with an xPath question

Need help with an xPath question

Scheduled Pinned Locked Moved XML / XSL
databasexmlhelptutorialquestion
3 Posts 2 Posters 0 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.
  • G Offline
    G Offline
    Grapes R Fun
    wrote on last edited by
    #1

    Hi Guys, I am not that good with xPath, so please don't laugh at my question :-) Trying to query an XmlNode to find a given element's parent's attribute... For example: Mike Rose Basically, I'm trying to find the name of "Mike's" parent (Mom). If you can help me out, thank you so much... thousand kisses to you! Grapes

    G 1 Reply Last reply
    0
    • G Grapes R Fun

      Hi Guys, I am not that good with xPath, so please don't laugh at my question :-) Trying to query an XmlNode to find a given element's parent's attribute... For example: Mike Rose Basically, I'm trying to find the name of "Mike's" parent (Mom). If you can help me out, thank you so much... thousand kisses to you! Grapes

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      You use parent::node():

      using namespace System;
      using namespace System::Xml;
       
      int main(array<System::String ^> ^args)
      {
      String^ xmlString =
      "<root>"
      " <parent name=\"Mom\">"
      " <kid>Mike</kid>"
      " <kid>Rose</kid>"
      " </parent>"
      "</root>";
       
      XmlDocument^ xmlDoc = gcnew XmlDocument;
      xmlDoc->LoadXml(xmlString);
       
      XmlNodeList^ children = xmlDoc->SelectNodes("/root/parent/kid");
       
      for each (XmlNode^ child in children)
      {
      //XmlNode^ parent = child->ParentNode;
       
      XmlNode^ parent = child->SelectSingleNode("parent::node()");
       
      if (parent != nullptr)
      {
      Console::WriteLine("{0}'s parent is good old {1}.", child->InnerText,
      parent->Attributes->GetNamedItem("name")->Value);
      }
       
      }
       
      return 0;
      }

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      G 1 Reply Last reply
      0
      • G George L Jackson

        You use parent::node():

        using namespace System;
        using namespace System::Xml;
         
        int main(array<System::String ^> ^args)
        {
        String^ xmlString =
        "<root>"
        " <parent name=\"Mom\">"
        " <kid>Mike</kid>"
        " <kid>Rose</kid>"
        " </parent>"
        "</root>";
         
        XmlDocument^ xmlDoc = gcnew XmlDocument;
        xmlDoc->LoadXml(xmlString);
         
        XmlNodeList^ children = xmlDoc->SelectNodes("/root/parent/kid");
         
        for each (XmlNode^ child in children)
        {
        //XmlNode^ parent = child->ParentNode;
         
        XmlNode^ parent = child->SelectSingleNode("parent::node()");
         
        if (parent != nullptr)
        {
        Console::WriteLine("{0}'s parent is good old {1}.", child->InnerText,
        parent->Attributes->GetNamedItem("name")->Value);
        }
         
        }
         
        return 0;
        }

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        G Offline
        G Offline
        Grapes R Fun
        wrote on last edited by
        #3

        Thanks alot George!!

        Grapes

        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