开发者

convert object to xml

开发者 https://www.devze.com 2023-02-17 06:02 出处:网络
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

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".

Below is my 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; }
    }    
  }  
}

a开发者_开发技巧nd the other class file is pasted below

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<Details> details;

    [XmlArray("Details")]
    [XmlArrayItem("details")]
   // public List<Details> details = new List<Details>();
    public List<Details> Details
    {
        get { return details; }
        set { details = value; }
    }
}
}

Below is the code where i am getting the error

  List<BillDetails> billlist = new List<BillDetails>();    
  public int x;    
  List<Details> newdetails = new List<Details>();    

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<BillDetails> billlist)
    {
        XmlSerializer myserializer = new XmlSerializer(typeof(BillDetails));
        TextWriter mywriter = new StreamWriter("C:\\billlist.xml");
        myserializer.Serialize(mywriter, billlist);--- Error is thrown here
        mywriter.Close();
    }

Please help to correct me.


The error is because billlist is not of type BillDetails but List<BillDetails>

you could try

XmlSerializer myserializer = new XmlSerializer(typeof(List<BillDetails>));


The error that stands out to me is that you're telling the XmlSerializer that it's type is BillDetails when it's really a list of BillDetails.


Have you tried constructing your serializer with List<BillDetails> type (you will serialize it, why do you put simple BillDetails into ctor?):

XmlSerializer myserializer = new XmlSerializer(typeof(List<BillDetails>));


XmlSerializer myserializer = new XmlSerializer(typeof(BillDetails));

you are constructing a serializer for the type BillDetails, but then passing it a List<BillDetails> - the serializer can't deal with that.

0

精彩评论

暂无评论...
验证码 换一张
取 消