tags:

views:

28

answers:

1
+1  Q: 

XLINQ Filtering

How do I take this XML and filter out the menu element that is marked as expert using XLINQ and return the rest?

<data>
   <preferences />
   <menus>
      <menu type="expert">
         <sub />
      </menu>
      <menu type="beginner">
         <sub />
      </menu>
   </menus>
</data>

So the result would be:

<data>
   <preferences />
   <menus>
      <menu type="beginner">
         <sub />
      </menu>
   </menus>
</data>
+2  A: 
        // parse the DOM if needed
        XDocument doc =XDocument.Parse(xml);
        // find the expert menu items
        var remove = doc.Descendants("menu").Where(
            menu => (string)menu.Attribute("type") == "expert").ToList();
        // remove them
        foreach (var item in remove) {
            item.Remove();
        }
        // to get output
        string s = doc.ToString();
Marc Gravell