Solved

xpath navigation in c#

Posted on 2006-11-14
3
1,192 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
  • 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

24 Experts available now in Live!

Get 1:1 Help Now