Created
May 6, 2011 20:02
-
-
Save PilotBob/959677 to your computer and use it in GitHub Desktop.
Looking for cleaner LINQ to XML solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public System.Xml.XmlDocument BuildMenu(System.Xml.XmlDocument xdoc) | |
{ | |
ApplicationLog.WriteTrace("AmsiWebMenuProvider::BuildMenu -- start"); | |
var context = new Amsi.Model.Config.ConfigEntities(); | |
Guid currentDatabase = Guid.Parse(UserState.DefaultDatabase); | |
var databaseRecords = (from database in context.Databases.Include("EvolutionModule") | |
where database.ID_DatabaseSet == currentDatabase | |
select database).ToList<Amsi.Model.Config.Database>(); | |
List<XmlNode> nodesToRemove = new List<XmlNode>(); | |
foreach (XmlNode node in xdoc.DocumentElement.ChildNodes) | |
{ | |
if (nodeText != "Home" && nodeText != "System") | |
{ | |
if (!databaseRecords.Any<Amsi.Model.Config.Database>(item => item.EvolutionModule.Name == nodeText)) | |
nodesToRemove.Add(node); | |
} | |
} | |
foreach (var node in nodesToRemove) | |
{ | |
xdoc.DocumentElement.RemoveChild(node); | |
} | |
ApplicationLog.WriteTrace("AmsiWebMenuProvider::BuildMenu -- end"); | |
return xdoc; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above code takes and Xml document that contains a list of menu items and needs to remove any menu items that are not contained in the databaseRecords list.
The above code works, however, I don't like the two loops. I expect there is a more elegant and readable LINQ to XML way to do this.
thanks.