Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C# Xpath query help!!

Posted on 2003-03-28
3
Medium Priority
?
364 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
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

572 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