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>Titan12 3</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.Sta rtupPath + @"\device.xm l";
// open XmlTextReader
....
int nMaxId = 0;
// get maximum devices within XML script
while(xmlDevice .Read())
{
if (xmlDevice.IsSt artElement() && xmlDevice.Name == "Device")
{
int nId = Int32.Parse(xml Device.GetAttri bute("id"));
if (nMaxId < nId)
nMaxId = nId;
}
}
}
// Execute XPath here
XPathDocument xdoc = new XPathDocument(d evicePath);
XPathNavigator nav = xdoc.CreateNavi gator();
// loop into 2 possible devices and extract particular information
for (int i = 1; i < nMaxId + 1; i++)
{
XPathNodeIterat or nodeItor = nav.Select(
"DeviceList/Device[@id='" + i + "']/Connection");
nodeItor.MoveNe xt();
TraverseChildre n(nodeItor);
}
....
// from the article
private void TraverseChildre n(XPathNodeIter ator nodeItor)
{
XPathNodeIterat or igor = nodeItor.Clone( );
igor.Current.Mo veToFirstChild( );
bool more = false;
do
{
PrintNode(igor. Current);
more = igor.Current.Mo veToNext();
}while(more);
}
private void PrintNode(XPath Navigator nav)
{
MessageBox.Show ("Value: " + nav.Value +
" Type : " + nav.NodeType.To String());
}
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.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com