Serialize an object to XML

后端 未结 17 1896
渐次进展
渐次进展 2020-11-22 04:41

I have a C# class that I have inherited. I have successfully \"built\" the object. But I need to serialize the object to XML. Is there an easy way to do it?

It looks

17条回答
  •  情歌与酒
    2020-11-22 05:36

    All upvoted answers above are correct. This is just simplest version:

    private string Serialize(Object o)
    {
        using (var writer = new StringWriter())
        {
            new XmlSerializer(o.GetType()).Serialize(writer, o);
            return writer.ToString();
        }
    }
    

提交回复
热议问题