Serialize an object to XML

后端 未结 17 1903
渐次进展
渐次进展 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:20

    You have to use XmlSerializer for XML serialization. Below is a sample snippet.

     XmlSerializer xsSubmit = new XmlSerializer(typeof(MyObject));
     var subReq = new MyObject();
     var xml = "";
    
     using(var sww = new StringWriter())
     {
         using(XmlWriter writer = XmlWriter.Create(sww))
         {
             xsSubmit.Serialize(writer, subReq);
             xml = sww.ToString(); // Your XML
         }
     }
    

提交回复
热议问题