Skip to content

Instantly share code, notes, and snippets.

@PilotBob
Created May 6, 2011 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PilotBob/959677 to your computer and use it in GitHub Desktop.
Save PilotBob/959677 to your computer and use it in GitHub Desktop.
Looking for cleaner LINQ to XML solution
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;
}
@PilotBob
Copy link
Author

PilotBob commented May 6, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment