Need advice about xpath resolving backwards

J

janhm

Hey, I had a problem with adding xml into a treeview few month ago. The
project got abandon, but im now working with it again.

Here is my problem:

I have an xml file containing:

<?xml version="1.0"?>
<kiosks>
<kiosk>
<kiosknavn>KIOSK001</kiosknavn>
<serienummer>001001001001</serienummer>
</kiosk>
<kiosk>
<kiosknavn>KIOSK002</kiosknavn>
<serienummer>002002002002</serienummer>
</kiosk>

and so on.

I then fill a treeview with all the <kiosknavn> in the xml. No problems
there...

Now I want to be able to search in the <kiosknavn> and have the content
displayed in another treeview.

If I search/click in treeview1 for KIOSK002 then I want the "KIOSK002" and
"002002002002" to be added into this second treeview.

I tried this :

treeView2.Nodes.Clear();
XmlDocument xDoc = new XmlDocument();
xDoc.Load("c://test.xml");
XmlNodeList nodes = xDoc.GetElementsByTagName("kiosknavn");
foreach(XmlNode node in nodes)
{
if (node.FirstChild.FirstChild.Value == treeView1.SelectedNode.Text)
{
treeView2.Nodes.Add(node.OuterXml);
}
}

But it only results in :

Object reference not set to an instance of an object.

Any advice?

Thanks
 
G

Guest

looks like the only problem is that you're drilling too deep off of you
<kiosknavn> node. Should work if you just use the following code in your
loop...

foreach(XmlNode node in nodes){
if (node.FirstChild.Value == treeView1.SelectedNode.Text){
treeView2.Nodes.Add(node.OuterXml);
}
}

hth,

_howard
 
J

Jan Mikkelsen

Thanks.. that helped :)

It dosn't error anymore. Now it outputs <kiosknavn>kioks002</kiosknavn> so
now i have something usable to work with.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

XPath querry... 2

Top