Need help with an xPath question
-
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
-
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
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
-
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
Thanks alot George!!
Grapes