title: How to create an XML document from a .NET object?
body: <p>I have the following variable that accepts a file name:</p>
  
  <pre><code>var xtr = new XmlTextReader(xmlFileName) { WhitespaceHandling = WhitespaceHandling.None };
  var xd = new XmlDocument();
  xd.Load(xtr);
  </code></pre>
  
  <p>I would like to change it so that I can pass in an object. I don't want to have to serialize the object to file first. </p>
  
  <p>Is this possible?</p>
  
  <p>Update:</p>
  
  <p>My original intentions were to take an xml document, merge some xslt (stored in a file), then output and return html... like this:</p>
  
  <pre><code>public string TransformXml(string xmlFileName, string xslFileName)
  {
       var xtr = new XmlTextReader(xmlFileName) { WhitespaceHandling = WhitespaceHandling.None };
       var xd = new XmlDocument();
       xd.Load(xtr);
  
       var xslt = new System.Xml.Xsl.XslCompiledTransform();
       xslt.Load(xslFileName);
       var stm = new MemoryStream();
       xslt.Transform(xd, null, stm);
       stm.Position = 1;
       var sr = new StreamReader(stm);
       xtr.Close();
       return sr.ReadToEnd();
  }
  </code></pre>
  
  <p>In the above code I am reading in the xml from a file. Now what I would like to do is just work with the object, before it was serialized to the file. </p>
  
  <p>So let me illustrate my problem using code</p>
  
  <pre><code>public string TransformXMLFromObject(myObjType myobj , string xsltFileName)
  {
       // Notice the xslt stays the same.
       // Its in these next few lines that I can't figure out how to load the xml document (xd) from an object, and not from a file....
  
       var xtr = new XmlTextReader(xmlFileName) { WhitespaceHandling = WhitespaceHandling.None };
       var xd = new XmlDocument();
       xd.Load(xtr);
  }
  </code></pre>