Solved

xpath using navigation c#

Posted on 2006-11-14
3
3,145 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
[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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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