convert class to xml string

Solutions on MaxInterview for convert class to xml string by the best coders in the world

showing results for - "convert class to xml string"
Mika
19 Nov 2020
1public string ToXML()
2    {
3        using(var stringwriter = new System.IO.StringWriter())
4        { 
5            var serializer = new XmlSerializer(this.GetType());
6            serializer.Serialize(stringwriter, this);
7            return stringwriter.ToString();
8        }
9    }
10
11 public static YourClass LoadFromXMLString(string xmlText)
12    {
13        using(var stringReader = new System.IO.StringReader(xmlText))
14        {
15            var serializer = new XmlSerializer(typeof(YourClass ));
16            return serializer.Deserialize(stringReader) as YourClass ;
17        }
18    }