Home > Uncategorized > XPath Expressions Examples using C#.Net

XPath Expressions Examples using C#.Net

Sample XML, save it as Sample.xml

<root>
<eConnect ACTION=”0″ Requester_DOCTYPE=”CusItem” DBNAME=”TWO” TABLENAME=”IV00101″DATE1=”1900-01-01T00:00:00″ ITEMNMBR=”100XLG”>
<CusItem>
<ITEMNMBR LotType =”1″>100XLG</ITEMNMBR>
<ITEMDESC>Green Phone</ITEMDESC>
<ITMSHNAM>Phone</ITMSHNAM>
<ITEMTYPE>1</ITEMTYPE>
<ITMGEDSC>Phone</ITMGEDSC>
<STNDCOST>55.50000</STNDCOST>
<CURRCOST>28.46000</CURRCOST>
</CusItem>
</eConnect>
<eConnect ACTION=”1″ Requester_DOCTYPE=”CusItem” DBNAME=”TWO” TABLENAME=”IV00101″ DATE1=”1900-01-01T00:00:00″ ITEMNMBR=”100XLGS”>
<CusItem>
<ITEMNMBR LotType =”2″>100XLGS</ITEMNMBR>
<ITEMDESC>Green Phone+</ITEMDESC>
<ITMSHNAM>Phone</ITMSHNAM>
<ITEMTYPE>2</ITEMTYPE>
<ITMGEDSC>AT</ITMGEDSC>
<STNDCOST>55.50000</STNDCOST>
<CURRCOST>28.46000</CURRCOST>
</CusItem>
</eConnect>
</root>

XmlDocument oXDoc = new XmlDocument();
oXDoc.Load(@”c:\Sample.xml”);
XmlNodeList oXNL;
XmlNode oXN;

//You can apply following operator
//or
//and
//=, !=
//<=, <, >=, >

//Applying expression in element, you would get a CusItem node with below xpath expression
oXNL = oXDoc.SelectNodes(“/root/eConnect/CusItem[ITEMNMBR != '' and ITEMTYPE != '2']“);

//Applying or and condition with presidence
oXNL = oXDoc.SelectNodes(“/root/eConnect[@ITEMNMBR = '100XLG' and (@ACTION = '0' or @ACTION = '1')]“);

//Applying expression on element and attribute, you would get a CusItem node with below xpath expression
oXNL = oXDoc.SelectNodes(“/root/eConnect[@ACTION = '1' and CusItem/ITEMTYPE = '2']“);

//Only got the first node
oXN = oXDoc.SelectSingleNode(“/root/eConnect/CusItem/STNDCOST[. = '55.50000']“);

//Getting attribute value of selected node
oXN = oXDoc.SelectSingleNode(“/root/eConnect/CusItem/ITEMNMBR[. = '100XLG']“);
string sTest = oXN.Attributes.GetNamedItem(“LotType”).InnerText;

//Applying contains fucntions, select only first node
oXN = oXDoc.SelectSingleNode(“/root/eConnect/CusItem[contains(ITEMNMBR, '100XLG')]“);

//Applying contains fucntions on element, this will work as like opertor
oXNL = oXDoc.SelectNodes(“/root/eConnect/CusItem[contains(ITEMNMBR, '100XLG')]“);

//Applying contains fucntions on attributes, this will work as like opertor
oXNL = oXDoc.SelectNodes(“/root/eConnect[contains(@ACTION, '1')]“);

You can use the following functions to work with strings.

Functions Returns
concat( ) The concatenation of its arguments. For example, concat (‘This is’, ‘ ‘, ‘my string’) returns “This is my string”
contains( ) An indication if one string contains the contents of a second strong. For example, contains (“This is my string”, “is my”) returns true, because the string “This is my string” does contain the string “is my”.
normalize-space( ) The argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space.
starts-with( ) An indication whether a string starts with the contents of a second string. For example, starts-with (“This is my string”, “This”) returns true , because the string “This is my string” does start with the string “This” .
string( ) Converts any value to a string.
string-length( ) The number of characters in the string, including spaces.
substring( ) Takes a specified number of characters out of a string. For example, substring (‘This is my string’ ,9, 2 ) returns my because we told it to start at the ninth character and take two characters.
substring-after( ) All the characters after a certain character. For example, substring-after (‘This is my string’,'s’ ) returns all the characters after the first occurrence of the letter “s”, in other words “is my string”.
substring-before( ) All the characters before a certain character. For example, substring-before (‘This is my string’,'s’ ) returns all the characters before the first occurrence of the letter “s”, in other words “Thi”.
translate (string, to-match, replace-with ) The string with all occurrences of the substring to-match replaced with the string replace-with .

Some very useful articles are located at.

http://www.w3.org/TR/xpath

http://publib.boulder.ibm.com/infocenter/adiehelp/index.jsp?topic=/com.ibm.etools.xsl.source.doc/topics/rfunct.html

Above table is also referred from above link.

http://support.microsoft.com/kb/308333

,

Comments (Close):1

Leave a Reply
  1. Imran 08/07/22

    Nice Information

TOP