XPath Problems.. need help!

G

Guest

Hi,

I read from this tutorial at codeproject

Question A:

http://www.codeproject.com/csharp/GsXPathTutorial.asp

regarding xpath.. but i try to apply in my situation, and can't get it work...

just say this is my xml file:

Device.xml
=========
<?xml version="1.0" encoding="utf-8"?>
<DeviceList xmlns="http://tempuri.org/Device.xsd">
<!-- Device 1 -->
<Device id="1">
<Connection>
<Type>Titan123</Type>
<Open />
<ConfigCard>
<Name>Titan Suprema</Name>
<Length>13</Length>
</ConfigCard>
</Connection>
</Device>
<Device id="2">
<Connection>
<Type>BBB Hardware</Type>
<Open />
<ConfigCard>
<Name>BBB DH</Name>
<Length>6</Length>
</ConfigCard>
</Connection>
</Device>
</DeviceList>

In my C# code,

string devicePath = Application.StartupPath + @"\device.xml";
// open XmlTextReader
....
int nMaxId = 0;

// get maximum devices within XML script
while(xmlDevice.Read())
{
if (xmlDevice.IsStartElement() && xmlDevice.Name == "Device")
{
int nId = Int32.Parse(xmlDevice.GetAttribute("id"));

if (nMaxId < nId)
nMaxId = nId;
}
}
}

// Execute XPath here
XPathDocument xdoc = new XPathDocument(devicePath);
XPathNavigator nav = xdoc.CreateNavigator();

// loop into 2 possible devices and extract particular information
for (int i = 1; i < nMaxId + 1; i++)
{
XPathNodeIterator nodeItor = nav.Select(
"DeviceList/Device[@id='" + i + "']/Connection");
nodeItor.MoveNext();

TraverseChildren(nodeItor);
}
....

// from the article
private void TraverseChildren(XPathNodeIterator nodeItor)
{
XPathNodeIterator igor = nodeItor.Clone();
igor.Current.MoveToFirstChild();
bool more = false;
do
{
PrintNode(igor.Current);
more = igor.Current.MoveToNext();
}while(more);
}

private void PrintNode(XPathNavigator nav)
{
MessageBox.Show("Value: " + nav.Value +
" Type : " + nav.NodeType.ToString());
}

From this solution:

i get this instead:

MessageBox1 - Value: Titan123 Titan Suprema 13BBB Hardware BBB DH 6
MessageBox2 - Value: Titan123 Titan Suprema 13BBB Hardware BBB DH 6
(repeat the same message - why?)

Any help please?

I want to get the Output of this:

MessageBox1 - Value: Titan123 Titan Suprema 13
MessageBox2 - Value: BBB Hardware BBB H 6

Question B:

By the way how can i get the value of individual Type example,

showing the output here:

Type - Titan123
Name - Titan Suprema
Length - 13

i always only manage to get the type which is "Name" and the whole message
"Titan123 Titan Suprema 13" instead separate data.

Any help please? Thanks.
 
D

Dan Bass

Chua Wen Ching,

This seems a little overkill for what you are trying to do...

I wrote a short, but complete application that does more what I think you
want:

using System;
using System.Xml;
using System.Xml.XPath;

namespace XmlXpathParser
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// load the xml into the dom
XmlDocument myDoc = new XmlDocument();
myDoc.Load ( @"XmlFile1.xml" );

// select all deviceID nodes
XmlNodeList nodes = myDoc.SelectNodes ( "DeviceList/Device" );

// display details on each
foreach ( XmlNode deviceNode in nodes )
{
DisplayDeviceNode ( deviceNode );
}

// wait for user response before closing
Console.WriteLine ( "----- press any key" );
Console.Read();


}

static void DisplayDeviceNode ( XmlNode deviceNode )
{
string type = deviceNode.SelectSingleNode (
"Connection/Type" ).InnerText;
string name = deviceNode.SelectSingleNode (
"Connection/ConfigCard/Name" ).InnerText;
string length = deviceNode.SelectSingleNode (
"Connection/ConfigCard/Length").InnerText;

Console.WriteLine ( "type = {0}, name = {1}, length = {2}",
type, name, length );
}
}
}



All you need to add is some error checking for if nodes don't exist, and
exception handling for catching problems if data is corrupted.
Beware the hard coded string for the xml file... All I did was cut and paste
your xml into "XmlFile1.xml".

HTH.

Dan.
 
G

Guest

Hi Dan Bass,

Thanks alot. It reallys helps me. But i do notice that it seems to process
a lot unlike xpath, well it is fast.. but i can't get what i want earlier (in
my previous email).

Can i do with xpath? Any tips? i heard xpath is much faster.

Cheers. Hooray :)
 
G

Guest

Hi Dan,

2 more things to add on.

exception handling for catching problems if data is corrupted
--> Why do i need to catch problems if data is corrupted? Any examples? Coz
i will be writing the script in xml manually, and process it in C#.

Beware the hard coded string for the xml file
--> I don't get your meaning here. Hard coded string ???

Cheers.
 
D

Dan Bass

In the example I'm using it is done using XPath references... The functions
SelectNodes and SelectSingleNode take in an XPath string.

2 more things to add on.

exception handling for catching problems if data is corrupted
--> Why do i need to catch problems if data is corrupted? Any examples?
Coz
i will be writing the script in xml manually, and process it in C#.

around all this you need to place "try {} catch {} finally" statements to
catch errors that may occur.
www.c-sharpcorner.com/ExceptionHandling.asp

Beware the hard coded string for the xml file
--> I don't get your meaning here. Hard coded string ???

I was simply refering the line
: myDoc.Load ( @"XmlFile1.xml" );
make sure the filename is correct for what you need.

Let me know if you need more help.

Thanks.

Dan.
 
G

Guest

Oh i see...

Thanks Dan Bass. It really helped me a lot. Now i am on the right track :)
Cheers.
 

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

Top