I have an XML document as follows:
<Menu>
<Group>
<Item Text="About Us" AccessRoles="Al l">
<Group>
<Item Text="Option 1" AccessRoles="Al l" />
<Item Text="Option 2" AccessRoles="Al l" />
<Item Text="Option 3" AccessRoles="Al l" />
</Group>
</Item>
<Item Text="Projects" AccessRoles="Al l">
<Group>
<Item Text="Option 1" AccessRoles="Al l" />
<Item Text="Option 2" AccessRoles="Em ployee" />
<Item Text="Option 3" AccessRoles="Ma nager,Executive " />
</Group>
</Item>
<Item Text="Library" AccessRoles="Em ployee,Manager, Executive">
<Group>
<Item Text="Option 1" AccessRoles="Em ployee" />
<Item Text="Option 2" AccessRoles="Ma nager" />
<Item Text="Option 3" AccessRoles="Ex ecutive" />
</Group>
</Item>
</Group>
</Menu>
This xml is used for telerik RADMenu component and before I use it with
the control I want to read the document and remove any menu options
(<Item>) that the current logged in user does not have roles for.
At the point of reading/parsing the document I have a comma-delimited
string (or ArrayList) of roles assigned to the current user (including
role "All") and so I need to check to see if any of the roles exist in
the AccessRoles attribute for each item.
The problem I am having is in removing the nodes that do not meet the
criteria.
I have been going down the following route (please note that this code
is only checking for the "All" role)...
Public Shared Function GetRoleBasedMen uXml() As String
Dim strMenuXml As String
Dim xmldocMenu As XmlDocument
strMenuXml = HttpContext.Cur rent.Cache.Get( "PortMenuXM L")
xmldocMenu = New XmlDocument
xmldocMenu.Load Xml(strMenuXml)
Dim xmlnlMenuItem As XmlNodeList =
xmldocMenu.GetE lementsByTagNam e("Item")
For Each xmlNode As XmlNode In xmlnlMenuItem
If Not xmlNode.Attribu tes("AccessRole s") Is Nothing Then
If xmlNode.Attribu tes("AccessRole s").Value <> "All" Then
xmlNode.RemoveA ll()
xmlNode.RemoveC hild(xmlNode.Pa rent)
End If
End If
Next
Dim swFormattedMenu XML As StringWriter
swFormattedMenu XML = New StringWriter
xmldocMenu.Save (swFormattedMen uXML)
Return swFormattedMenu XML.ToString()
End Function
The problem with this is that the RemoveAll() method only removes the
child nodes and the current node attributes leaving <Item></Item>. I
have tried to use xmlNode.ParentN ode.RemoveChild (xmlNode) after the
RemoveAll but this is invalid in a For Each loop! I have spent most of
my day trying to find different ways around this problem (XPath etc.)
but because it is variable depth and also the nature of the AccessRoles
validation it has proved unfruitful - I am missing something!
Any help/suggestions would be greatly appreciated!