Solved

xpath navigation in c#

Posted on 2006-11-14
3
1,197 Views
Last Modified: 2008-01-09
I have an xml document like this

<?xml version="1.0" encoding="utf-8" ?>
<!-- This file defines the directory structure for registration of new content providers.-->
<!-- Change text value of DIRECTORY nodes to create the directory structure needed.-->
<ROOT KEY="" VALUE="" DESCRIPTION="">
      <DIRECTORY KEY="CPChannels" VALUE="Channels" DESCRIPTION="">
            <DIRECTORY KEY="CPChannelsMyChannels" VALUE="MyChannels" DESCRIPTION=""/>
            <DIRECTORY KEY="CPChannelsMyMusic" VALUE="MyMusic" DESCRIPTION=""/>
            <DIRECTORY KEY="CPChannelsTest" VALUE="Test" DESCRIPTION="">
                  <DIRECTORY KEY="CPChannelsTestTestTest1" VALUE="TestTest1" DESCRIPTION=""/>
                  <DIRECTORY KEY="CPChannelsTestTestTest2" VALUE="TestTest2" DESCRIPTION=""/>
            </DIRECTORY>
      </DIRECTORY>
      
      <DIRECTORY KEY="CPMediaFiles" VALUE="MediaFiles" DESCRIPTION="">
            <DIRECTORY KEY="CPMediaFilesAds" VALUE="Ads" DESCRIPTION="">
                  <DIRECTORY KEY="CPMediaFilesAdsTestTest1" VALUE="TestTest1" DESCRIPTION=""/>
                  <DIRECTORY KEY="CPMediaFilesAdsTestTest2" VALUE="TestTest2" DESCRIPTION=""/>
            </DIRECTORY>
            <DIRECTORY KEY="CPMediaFilesMedia" VALUE="Media" DESCRIPTION=""/>
            <DIRECTORY KEY="CPMediaFilesProgram" VALUE="Program" DESCRIPTION=""/>
      </DIRECTORY>      
      
      <DIRECTORY KEY="CPPackagedVideos" VALUE="PackagedVideos" DESCRIPTION="">
            <DIRECTORY KEY="CPPackagedVideosAds" VALUE="Ads" DESCRIPTION=""/>
            <DIRECTORY KEY="CPPackagedVideosMedia" VALUE="Media" DESCRIPTION=""/>
            <DIRECTORY KEY="CPPackagedVideosProgram" VALUE="Program" DESCRIPTION=""/>
      </DIRECTORY>
      
      <DIRECTORY KEY="CPSponsors" VALUE="Sponsors" DESCRIPTION="">
            <DIRECTORY KEY="CPSponsorsImages" VALUE="Images" DESCRIPTION=""/>
      </DIRECTORY>
      
      <DIRECTORY KEY="CPImages" VALUE="Images" DESCRIPTION="">
            <DIRECTORY KEY="CPImagesTest" VALUE="Test" DESCRIPTION=""/>
      </DIRECTORY>

</ROOT>


using c# I want to search the element where a particulay KEY attribute value is present. e.g. if KEY="CPChannelsTest" then the Xpath navigator should return the VALUE attribute of that node i.e. VALUE returned should be ChannelsTest .

Is it possible to return all the immediate Parents of this searched node. if yes how?
0
Comment
Question by:skvikram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17938680
XmlDocument doc = new XmlDocument();
doc.LoadXml("the xml above.")
XmlNode targetNode = doc.SelectSingleNode("//*[@KEY = 'CPChannelsTest']");
if (node != null){
     return.Node.Attributes["VALUE"].Value;
}else{
     return null;
}
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17938707
Sorry, more like :

XmlNode targetNode = doc.SelectSingleNode("//*[@KEY = 'CPChannelsTest']");
if (targetNode != null){
      Console.WriteLine(targetNode.Attributes["VALUE"].Value);
}else{
      Console.WriteLine("null");      
}
Console.ReadLine();
0
 
LVL 7

Author Comment

by:skvikram
ID: 17945010
thanks for quick reply. I figured it out myself also like this

string SearchString = "//DIRECTORY[@KEY ='" + Key + "']";
                  
XmlNode xnodWorking = xmlDoc.SelectSingleNode(SearchString);
                  

while (!(xnodWorking.NodeType == XmlNodeType.Document) )
{
      DirectoryPath =  xnodWorking.Attributes["VALUE"].Value+ "/" + DirectoryPath;
      xnodWorking = xnodWorking.ParentNode;
}

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question