You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to create an application. The
application contains the following XML fragment.
<ApplicationMenu>
<MenuItem name=”File”>
<MenuItem name=”New”>
<MenuItem name=”Project” />
<MenuItem name=”Web Site” />
</MenuItem>
<MenuItem name=”Open”>
<MenuItem name=”Project” />
<MenuItem name=”Web Site” />
</MenuItem>
<MenuItem name=”Save” />
</MenuItem>
<MenuItem name=”Edit”>
<MenuItem name=”Cut” />
<MenuItem name=”Copy” />
<MenuItem name=”Paste” />
</MenuItem>
<MenuItem name=”Help”>
<MenuItem name=”Help” />
<MenuItem name=”About” />
</MenuItem>
</ApplicationMenu>
The application queries the XML fragment by using the XmlDocument class. You need to select all
the descendant elements of the MenuItem element that has its name attribute as File. Which XPath
expression should you use?
A.
//*[@name=’File’][name()=’MenuItem’]
B.
/ApplicationMenu/MenuItem[‘File’]//MenuItem
C.
/ApplicationMenu/MenuItem/descendant::MenuItem[‘File’]
D.
/ApplicationMenu/MenuItem[@name=’File’]/descendant::MenuItem
Explanation:
XPath Examples:
XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem[@name=’File’]/descendant::
MenuItem”); // 01 == 02XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem[@name=’File’]//MenuItem”);
// 02 ==
01
XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem[2]/descendant::MenuItem”);
// 03
XmlNodeList nodes =
doc.SelectNodes(“/ApplicationMenu/MenuItem[last()]/descendant::MenuItem”); // 04
XmlNodeList nodes =
doc.SelectNodes(“/ApplicationMenu/MenuItem/descendant::MenuItem[/ApplicationMenu/
MenuItem/@name=@name]”); // 05
XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem/*”); // 06
XmlNodeList nodes =
doc.SelectNodes(“/ApplicationMenu/MenuItem/descendant::*[@Description]”); //07
XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem[MenuItem][position()=2]”); //
08 == 09
XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem[MenuItem and
position()=2]”); // 09 == 08
XmlNodeList nodes = doc.SelectNodes(“/ApplicationMenu/MenuItem[SubMenu or position()=2]”); //
10
XPath Examples
(http://msdn.microsoft.com/en-us/library/ms256086.aspx)