title: How do I filter XML nodes and its child nodes in a XML document?
body: <p>So I have a XML document in which I get as a HttpWebResponse from an API I'm working with. However, the document in which I get is about 166 thousand rows long and includes a lot of unnecessary data in which I don't need. I've read the API documentation and it doesn't seem that there is a filtering attribute in which suits my needs. So I figured that I would filter it programmatically but I haven't been able to succeed on that part. So the XML document looks a bit like this:</p>
  
  <pre><code>&lt;sensortree&gt;
          &lt;nodes&gt;
              &lt;group id=""&gt;
                  &lt;name&gt;&lt;/name&gt;
                  &lt;id&gt;&lt;/id&gt;
                  &lt;url&gt;&lt;/url&gt;
                  &lt;tags&gt;&lt;/tags&gt;
                  &lt;priority&gt;&lt;/priority&gt;
                  &lt;fixed&gt;&lt;/fixed&gt;
                  &lt;hascomment&gt;&lt;/hascomment&gt;
                  &lt;status_raw&gt;&lt;/status_raw&gt;
                  &lt;active&gt;&lt;/active&gt;
                  &lt;probenode id=""&gt;
                      &lt;name&gt;&lt;/name&gt;
                      &lt;id&gt;&lt;/id&gt;
                      &lt;url&gt;&lt;/url&gt;
                      &lt;tags&gt;&lt;/tags&gt;
                      &lt;priority&gt;&lt;/priority&gt;
                      &lt;fixed&gt;&lt;/fixed&gt;
                      &lt;hascomment&gt;&lt;/hascomment&gt;
                      &lt;status_raw&gt;&lt;/status_raw&gt;
                      &lt;active&gt;&lt;/active&gt;
                      &lt;group id=""&gt;
                          &lt;name&gt;&lt;/name&gt;
                          &lt;id&gt;&lt;/id&gt;
                          &lt;url&gt;&lt;/url&gt;
                          &lt;tags&gt;&lt;/tags&gt;
                          &lt;priority&gt;&lt;/priority&gt;
                          &lt;fixed&gt;&lt;/fixed&gt;
                          &lt;hascomment&gt;&lt;/hascomment&gt;
                          &lt;status_raw&gt;&lt;/status_raw&gt;
                          &lt;active&gt;&lt;/active&gt;
                          &lt;group id=""&gt;
                              &lt;name&gt;&lt;/name&gt;
                              &lt;id&gt;&lt;/id&gt;
                              &lt;url&gt;&lt;/url&gt;
                              &lt;tags&gt;&lt;/tags&gt;
                              &lt;priority&gt;&lt;/priority&gt;
                              &lt;fixed&gt;&lt;/fixed&gt;
                              &lt;hascomment&gt;&lt;/hascomment&gt;
                              &lt;status_raw&gt;&lt;/status_raw&gt;
                              &lt;active&gt;&lt;/active&gt;
                              &lt;group id=""&gt;
                                  &lt;name&gt;&lt;/name&gt;
                                  &lt;id&gt;&lt;/id&gt;
                                  &lt;url&gt;&lt;/url&gt;
                                  &lt;tags&gt;&lt;/tags&gt;
                                  &lt;priority&gt;&lt;/priority&gt;
                                  &lt;fixed&gt;&lt;/fixed&gt;
                                  &lt;hascomment&gt;&lt;/hascomment&gt;
                                  &lt;status_raw&gt;&lt;/status_raw&gt;
                                  &lt;active&gt;&lt;/active&gt;
                                  &lt;device id=""&gt;
                                      &lt;summary&gt;&lt;/summary&gt;
                                      &lt;name&gt;&lt;/name&gt;
                                      &lt;deviceicon&gt;&lt;/deviceicon&gt;
                                      &lt;id&gt;&lt;/id&gt;
                                      &lt;url&gt;&lt;/url&gt;
                                      &lt;tags&gt;&lt;/tags&gt;
                                      &lt;priority&gt;&lt;/priority&gt;
                                      &lt;fixed&gt;&lt;/fixed&gt;
                                      &lt;hascomment&gt;&lt;/hascomment&gt;
                                      &lt;host&gt;&lt;/host&gt;
                                      &lt;status_raw&gt;&lt;/status_raw&gt;
                                      &lt;active&gt;&lt;/active&gt;
                                      &lt;sensor id=""&gt;
                                          &lt;name&gt;&lt;/name&gt;
                                          &lt;id&gt;&lt;/id&gt;
                                          &lt;url&gt;&lt;/url&gt;
                                          &lt;tags&gt;&lt;/tags&gt;
                                          &lt;priority&gt;&lt;/priority&gt;
                                          &lt;fixed&gt;&lt;/fixed&gt;
                                          &lt;hascomment&gt;&lt;/hascomment&gt;
                                          &lt;sensortype&gt;&lt;/sensortype&gt;
                                          &lt;sensorkind&gt;&lt;/sensorkind&gt;
                                          &lt;interval&gt;&lt;/interval&gt;
                                          &lt;status_raw&gt;&lt;/status_raw&gt;
                                          &lt;status&gt;&lt;/status&gt;
                                          &lt;datamode&gt;&lt;/datamode&gt;
                                          &lt;lastvalue&gt;&lt;/lastvalue&gt;
                                          &lt;lastvalue_raw&gt;&lt;/lastvalue_raw&gt;
                                          &lt;statusmessage&gt;&lt;/statusmessage&gt;
                                          &lt;active&gt;&lt;/active&gt;
                                      &lt;/sensor&gt;
                                  &lt;/device&gt;
                              &lt;/group&gt;
                          &lt;/group&gt;
                      &lt;/group&gt;
                  &lt;/probenode&gt;
              &lt;/group&gt;
          &lt;/nodes&gt;
  &lt;/sensortree&gt;
  </code></pre>
  
  <p>Now you might notice that all elements are empty, that is because I have deleted all inner texts of the elements as I cannot share that information with the public, although I asure you that all elements have inner texts in reality. Now each device also have multiple sensors but they all look the same so I only included one. All the group elements also have a different structure of child nodes so each group is not like the other. Now I only need the elements "nodes", "group", "probenode", "device", "sensor", "name" and "id". The rest of the elements needs to be deleted from the document while I still keep the hierarchical structure of the document. So in short what I want to do is loop through all the nodes and each nodes child nodes, see if the element is an element I want to keep and delete it if it isn't. Another twist is that I don't know if XPath is an option because the structure is not the same everywhere. Things I have tried are the following.</p>
  
  <pre><code>//Method 1 - It doesn't delete the nodes that are not specified, I think it's because it doesn't check the childnodes because it does delete other nodes.
  foreach (XmlNode node in xDoc)
              {
                  if (node.Name == "group" || node.Name == "probenode" ||     node.Name == "device" || node.Name == "sensor")
                  {
                  }
                  else
                  {
                      node.ParentNode.RemoveChild(node);
                  }
              }
  
  //Method 2 - Not sure how to delete a node from a XmlReader instance 
  StringWriter sw = new StringWriter();
              XmlTextWriter xw = new XmlTextWriter(sw);
              xDoc.WriteTo(xw);
              String xmlString = sw.ToString();
  
              XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xmlString));
              while (rdr.Read())
              {
                  if (rdr.NodeType == XmlNodeType.Element)
                  {
                      if (rdr.LocalName == "group" || rdr.LocalName == "probenode" || rdr.LocalName == "device" || rdr.LocalName == "sensor"
                      || rdr.LocalName == "name" || rdr.LocalName == "lastvalue" || rdr.LocalName == "status")
                      {
  
                      }
                      else
                      {
  
                      }
                  }
              }
  </code></pre>
  
  <p>Now this is what I want my xml document to look like after it's formatted:</p>
  
  <pre><code>&lt;sensortree&gt;
          &lt;nodes&gt;
              &lt;group id=""&gt;
                  &lt;name&gt;&lt;/name&gt;
                  &lt;id&gt;&lt;/id&gt;
                      &lt;probenode id=""&gt;
                      &lt;name&gt;&lt;/name&gt;
                      &lt;id&gt;&lt;/id&gt;
                      &lt;group id=""&gt;
                          &lt;name&gt;&lt;/name&gt;
                          &lt;id&gt;&lt;/id&gt;
                              &lt;name&gt;&lt;/name&gt;
                              &lt;id&gt;&lt;/id&gt;
                              &lt;group id=""&gt;
                                  &lt;name&gt;&lt;/name&gt;
                                  &lt;id&gt;&lt;/id&gt;
                                  &lt;device id=""&gt;
                                      &lt;name&gt;&lt;/name&gt;
                                      &lt;id&gt;&lt;/id&gt;
                                      &lt;sensor id=""&gt;
                                          &lt;name&gt;&lt;/name&gt;
                                          &lt;id&gt;&lt;/id&gt;
                                      &lt;/sensor&gt;
                                  &lt;/device&gt;
                              &lt;/group&gt;
                          &lt;/group&gt;
                      &lt;/group&gt;
                  &lt;/probenode&gt;
              &lt;/group&gt;
          &lt;/nodes&gt;
  &lt;/sensortree&gt;
  </code></pre>