I have unusual format of XML as below example
<mainmenu>
<menu caption="File">
<menuitem caption="New" tooltip="Create New File" shortcut="Ctrl-N" Action="New">
<menuitem caption="Open" tooltip="Open Existing File" shortcut="Ctrl-O" Action="Open">
<menu caption="Import">
<menuitem caption="As New File" tooltip="Import To New Sheet" shortcut="F11" Action="ImportNew">
<menuitem caption="As Current File" tooltip="Import To Current Active Sheet" shortcut="F12" Action="ImportOpen">
</menu>
<menuitem caption="Exit" tooltip="Exit Program" shortcut="Ctrl-Q" Action="Exit">
</menu>
<menu caption="Edit">
<menuitem caption="Cut" tooltip="" shortcut="Ctrl-C" Action="Cut">
<menuitem caption="Copy" tooltip="" shortcut="Ctrl-X" Action="Copy">
<menuitem caption="Paste" tooltip="" shortcut="Ctrl-V" Action="Paste">
</menu>
</mainmenu>
I need above XML to be parsed into tabular data view with dataset and then can be retrieved from a function, for example:
getData("Edit")
result:
caption tooltip shortcut action
cut ...
copy ...
paste ...
Another example
getData("File.Import")
result:
caption tooltip shortcut action
as new file ...
as current file ...
So, what is the best method to parse the XML? Thanks.
Linq to Xml would be a good way to go for this. You can select menu elements based on the criteria, then select a collection of whatever data type you need based on the attributes of the menuitem elements. I wrote out an example below, which implements the simple example getData(“Edit”) and populates a DataTable with the child menu items (in your example, Cut, Copy, Paste).
It should be possible to extend this approach to select nested menu items based on “File.Import”, etc, as required.