Solved

xpath navigation in c#

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Producing decimal places when it shouldn;t be 8 42
Connection String 16 54
Page navigation in windows phone 8.1 application not working. 5 24
Coding C# in Linux 8 60
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

806 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