Solved

xpath using navigation c#

Posted on 2006-11-14
3
3,141 Views
Last Modified: 2011-08-18
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
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17938760
Hi skvikram,

 here is the XPath "//*[@KEY = 'CPChannelsTest']/@VALUE"

Cheers!
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 17938790
skvikram,

and here you find all on how to navigate XPaths in C#
http://www.codeproject.com/csharp/GsXPathTutorial.asp

cheers

Geert
0
 
LVL 7

Author Comment

by:skvikram
ID: 17945019
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now