using the Xpath

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have XML file wich containes the folowing Data:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
- <span style="color:Blue; <<span style="color:#A31515; UpdaterMessagesAr<span style="color:Blue; >
- <span style="color:Blue; <<span style="color:#A31515; Message<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ID<span style="color:Blue; >1<span style="color:Blue; </<span style="color:#A31515; ID<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Caption<span style="color:Blue; >11<span style="color:Blue; </<span style="color:#A31515; Caption<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Descreption<span style="color:Blue; >111<span style="color:Blue; </<span style="color:#A31515; Descreption<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Message<span style="color:Blue; >
- <span style="color:Blue; <<span style="color:#A31515; Message<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ID<span style="color:Blue; >2<span style="color:Blue; </<span style="color:#A31515; ID<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Caption<span style="color:Blue; >22<span style="color:Blue; </<span style="color:#A31515; Caption<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Description<span style="color:Blue; >222<span style="color:Blue; </<span style="color:#A31515; Description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Message<span style="color:Blue; >

<span style="color:Blue; </<span style="color:#A31515; UpdaterMessagesAr<span style="color:Blue; >
[/code]
I tried this code to get the value of the caption of a message that got specific ID num<br/>
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; public <span style="color:Blue; bool ReadMessageDetailesFromXmlFile(<span style="color:Blue; int ID)
{
<span style="color:Blue; bool Result = <span style="color:Blue; false;
<span style="color:Blue; string Contents = <span style="color:Blue; string.Empty;
<span style="color:Blue; string WhatToRead = <span style="color:#A31515; "ID";
<span style="color:Blue; try
{
XPathDocument doc = <span style="color:Blue; new XPathDocument(Properties.Settings.Default.XMLFile_UpdaterMessages);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile(<span style="color:#A31515; "Caption[ID=" + ID.ToString() + <span style="color:#A31515; "]");
XPathNodeIterator iterator = nav.Select(expr);
XPathNavigator nav2 = iterator.Current.Clone();
<span style="color:Blue; this.MessageCaption = nav.Value;
}
<span style="color:Blue; catch (Exception ex)
{
Method.ShowExceptionMessage(ex);
Result = <span style="color:Blue; false;
}
<span style="color:Blue; return Result;
}
[/code]
<br/>
but it`s giving me the whole xml file contents<br/>


View the full article
 
Back
Top