Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing LINQ to filter XML file
    primarykey
    data
    text
    <p>I have an XML file that looks like this:</p> <pre><code>&lt;SiteMenuItems&gt; &lt;SiteMenuItem text="Home" navigateurl="/Default.aspx" tooltip="Return Home" /&gt; &lt;SiteMenuItem text="Admin" roles="Admin"&gt; &lt;SiteMenuItem text="User Admin" navigateurl="/Authorized/UserAdmin/Default.aspx" roles="SuperAdmin" /&gt; &lt;/SiteMenuItem&gt; &lt;/SiteMenuItems&gt; </code></pre> <p>Using the example I found at <a href="https://stackoverflow.com/questions/2824794/how-to-convert-database-hierarchical-data-to-xml-using-asp-net-3-5-and-linq">at this StackOverflow question</a>, I was able to come up with the following code:</p> <pre><code> public string GetMenu() { XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath(@"~/App_Data/SiteMenu.xml")); xmlDoc = FilterRoles(xmlDoc); //Working on role problem return ExecuteXSLTransformation(xmlDoc.ToString()); } private XDocument FilterRoles(XDocument xmlDoc) { XElement element = xmlDoc.Element("SiteMenuItems"); XElement root = new XElement("SiteMenuItems", ( from sm in element.Elements("SiteMenuItem") where UserHelper.IsUserAuthorized(sm.Attribute("roles")) select new XElement(sm)) ); XDocument menuXml = new XDocument(); menuXml.Add(root); return menuXml; } </code></pre> <p>The problem I have is that the code is only filtering the first level: </p> <pre><code>&lt;SiteMenuItem text="Admin" roles="Admin"&gt; </code></pre> <p>But I can't figure out how to filter the internal SiteMenuItem as well:</p> <pre><code>&lt;SiteMenuItem text="User Admin" navigateurl="/Authorized/UserAdmin/Default.aspx" roles="SuperAdmin" /&gt; </code></pre> <p>As it is now, if you have the Admin role, it also returns the "User Admin" item.</p> <p>[UPDATE] Thanks to woodings, my child nodes are now going through the "IsUserAuthorized" test.</p> <p>Unfortunately, even though the test is returning "false" for the "User Admin" node, that node is still getting returned as part of the overall XML document.</p> <p>I was hoping it would return something like this:</p> <pre><code>&lt;SiteMenuItems&gt; &lt;SiteMenuItem text="Home" navigateurl="/Default.aspx" tooltip="Return Home" /&gt; &lt;SiteMenuItem text="Admin" roles="Admin"&gt; &lt;/SiteMenuItem&gt; &lt;/SiteMenuItems&gt; </code></pre> <p>Any ideas?</p> <p>Thanks again!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload