title: How to serialize a .NET object and assign to a string variable?
body: <p>How do I serialize a .NET object into XML and assign the XML string to variable?</p>
  
  <p>Example:</p>
  
  <pre><code>private void SerializeObject(string filename)
  {
      Console.WriteLine("Writing With Stream");
  
      XmlSerializer serializer =
      new XmlSerializer(typeof(OrderedItem));
      OrderedItem i = new OrderedItem();
      i.ItemName = "Widget";
      i.Description = "Regular Widget";
      i.Quantity = 10;
      i.UnitPrice = (decimal) 2.30;
      i.Calculate();
  
      // Create a FileStream to write with.
      Stream writer = new FileStream(filename, FileMode.Create);
  
      // Serialize the object, and close the TextWriter
      serializer.Serialize(writer, i);
  
      //Assign the serialized XML to a variable here.
      //For example, var serialized=serializer.serialized(i);
  
      writer.Close();
  }
  </code></pre>