?
Solved

C# Xpath query help!!

Posted on 2003-03-28
3
Medium Priority
?
361 Views
Last Modified: 2007-12-19
Ok, i have been racking my brain on this for the past hour now..

i have an xml file

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <Page name="Date Select">
    <Controls>
        <ListControl name="LoanType" type="listbox">
          <item value="LIS" />
       <item value="LPS" />
        </ListControl>
        <ListControl name="Selection" type="listbox">
          <item value="Select Suspend Status(s)" />
       <item value="Select All" />
       <item value="Select All but Completed" />
        </ListControl>
     <ListControl name="SuspendStatus" type="listbox">
       <item value="1st Call" />
       <item value="2nd Call" />
       <item value="Escalate" />
       <item value="Completed" />
       <item value="On Hold" />
       <item value="Hold for Map" />
     </ListControl>
     <ListControl name="Command" type="dropdown">
       <item value="Suspend Status Report" />
       <item value="Filter by Date" />
     </ListControl>
     </Controls>
   </Page>
</root>

and i am passing an xpath nodetest to my program..


     private void PutXMLData()
     {
       XmlDocument xmldoc = new XmlDocument();
       xmldoc.Load(Server.MapPath("pagedata.xml"));
       XmlNode root = xmldoc.DocumentElement;
       try
       {
           //-----this is where the issue lies!
             XmlNodeList xnodelist = root.SelectNodes("/root/Page/Controls/ListControl    [@name='LoanType']");
             foreach(XmlNode xnode in xnodelist)
          {
              lst1.Items.Add(xnode.ChildNodes[0].value);
          }
         
          } catch (System.Xml.XPath.XPathException ex) {          
          lblError.Text = ex.Message.ToString();
       }
        }

when i run this.. it gives me nothing in my listbox, but when when i replace xnode.ChildNodes[0].value with xnode.ChildNodes[0].name, then it gives me the string
"item" in the listbox..so i think i am off to a correct start.

But from looking at the XML file, i would think it would put the string
"LIS" and "LPS" in my listbox..which is what I want.

how would I do this with an Xpath nodetest?

Thanks!

nazxul



0
Comment
Question by:nazxul0101
[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
3 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 8228101
Use InnerText property instead of Value property. This will work if all your nodes are element types and your values are not set as attributes of these nodes.
0
 

Accepted Solution

by:
mega_119 earned 1200 total points
ID: 8236876
Here is the way I do it. I test it with your XML and it is working

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("pagedata.xml");
XmlNode root = xmldoc.DocumentElement;
try
{
                   
XmlNode xnode = root.SelectSingleNode("/root/Page/Controls/ListControl[@name='LoanType']");
for (int i=0;  i< xnode.ChildNodes.Count; i++)
{
MessageBox.Show(xnode.ChildNodes[i].Attributes.Item(0).Value.ToString() );
}

The XPath Query you specified is correct but your for loop is wrong. you need to loop through the childNodes and get the attribute value not the node value since in your case you are storing the data in the attributes not the nodes.


Don't forget to "Accept My Answer" if my response was of help



0
 

Author Comment

by:nazxul0101
ID: 8240628
Great, thanks so much.. I didnt know i had to use item!
never would have thought of that..

i added 50, for the speed factor of your reply
thanks

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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