title: How to remove whitespace from XElement object created from XElement.ReadFrom(XmlReader)
body: <p>I am parsing a large xml file. So I am using an XmlReader in combination with XElement instead of XElement.Load(). </p>
  
  <p>I have created as XElement object from XmlReader as shown below and <a href="http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom.aspx" rel="nofollow noreferrer">here</a>.  </p>
  
  <pre><code>static IEnumerable&lt;XElement&gt; StreamRootChildDoc(string uri)
  {
      using (XmlReader reader = XmlReader.Create(uri, xmlReaderSettings))
      {
          reader.MoveToContent();
          // Parse the file and display each of the nodes.
          while (reader.Read())
          {
              switch (reader.NodeType)
              {
                  case XmlNodeType.Element:
                      if (reader.Name == "Child")
                      {
                          XElement el = XElement.ReadFrom(reader) as XElement;
                          if (el != null)
                              yield return el;
                      }
                      break;
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to save this XElement object content in the database as string without the whitespace. None of the below 3 ways work. Just a note, if I load the xml in memory using XElement.Load(), the ToString(SaveOptions.DisableFormatting) works. </p>
  
  <pre><code>&lt;root&gt;  &lt;child&gt;&lt;/child&gt;  &lt;/root&gt; //xml saved in db with whitespace
  &lt;root&gt;&lt;child&gt;&lt;/child&gt;&lt;/root&gt; //want to save as this
  
  XElement.ToString(SaveOptions.DisableFormatting) //
  XElement.ToString(SaveOptions.None)
  XElement.ToString()
  </code></pre>
  
  <p>The XmlReaderSettings I am using for the XmlReader object are below. I tried IgnoreWhitespace =true/false with no luck but I cannot set it as true as some elements are skipped (for reason for skipping, see <a href="https://stackoverflow.com/q/2299632/58678">Why does XmlReader skip every other element if there is no whitespace separator?</a>).</p>
  
  <pre><code>    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
      xmlReaderSettings.ProhibitDtd = false;
      //xmlReaderSettings.IgnoreWhitespace = true;//cannot use this setting
  </code></pre>
  
  <p>It works if I parse the XElement object but that defeats the whole purpose of using XmlReader as XElement.Parse() loads the xml in memory. </p>
  
  <pre><code>XElement el = XElement.ReadFrom(reader) as XElement;
  XElement.Parse(el.ToString(), LoadOptions.None)
  </code></pre>
  
  <p>How can I remove the whitespace?</p>
  
  <p><strong>Edit: This is what I had to do:</strong></p>
  
  <ol>
  <li>The elements skipping is due to two reads <code>reader.Read()</code> and <code>XElement.ReadFrom(reader)</code> in the same iteration thereby skipping every other element. Fixing the loop mentioned in the link above solves that. The issue has nothing to do with <code>XmlReaderSettings</code>. </li>
  <li><code>xelement.ToString(SaveOptions.DisableFormatting)</code> removes the pretty formatting.</li>
  </ol>