title: convert object to xml
body: <p>I need to convert an object to xml and have gone through few articles to do it. however, I am getting an error" Error generating an xml document. Make sure the source type is same as the destination type".</p>
  
  <p>Below is my code</p>
  
  <pre><code>namespace XYZ    {  
    [Serializable]  
  [XmlRoot("details")]    
  public class Details  
  {  
      private string code;
  
      [XmlAttribute ("code")]
      public string Code
      {
          get { return code; }
          set { code= value; }
      }
  
      private string mcode;
  
      [XmlAttribute("mcode")]
      public string MCode
      {
          get { return mcode; }
          set { mcode= value; }
      }    
    }  
  }
  </code></pre>
  
  <p>and the other class file is pasted below</p>
  
  <pre><code>namespace ABC
  {
  [Serializable]
  [XmlRoot("BillDetails")]
  public class BillDetails
  {
      private string cat;
  
      [XmlAttribute("Cat")]
      public string Cat
      {
          get { return cat; }
          set { cat= value; }
      }
  
      private string customername;
  
      [XmlAttribute("CustomerName")]
      public string CustomerName
      {
          get { return customername; }
          set { customername = value; }
      }
  
      private List&lt;Details&gt; details;
  
      [XmlArray("Details")]
      [XmlArrayItem("details")]
     // public List&lt;Details&gt; details = new List&lt;Details&gt;();
      public List&lt;Details&gt; Details
      {
          get { return details; }
          set { details = value; }
      }
  }
  }
  </code></pre>
  
  <p>Below is the code where i am getting the error </p>
  
  <pre><code>  List&lt;BillDetails&gt; billlist = new List&lt;BillDetails&gt;();    
    public int x;    
    List&lt;Details&gt; newdetails = new List&lt;Details&gt;();    
  
  public void Button1_Click(object sender, EventArgs e)
      {
        if (Session["x"] == null)
              {
                  newdetails.Add(new Details() { Code = Code.Text, MCode = MCode.Text});
                  billlist.Add(new BillDetails() { Cat = Cat.Text, Details = newdetails.ToList(), CustomerName = CustomerName.Text });
                  //Code to bindGrid                    
                  Session["x"] = newdetails;
                  serializetoxml(billlist);
  
              }
  
    private void serializetoxml(List&lt;BillDetails&gt; billlist)
      {
          XmlSerializer myserializer = new XmlSerializer(typeof(BillDetails));
          TextWriter mywriter = new StreamWriter("C:\\billlist.xml");
          myserializer.Serialize(mywriter, billlist);--- Error is thrown here
          mywriter.Close();
      }
  </code></pre>
  
  <p>Please help to correct me.</p>