Serialize an object to XML

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

        string FilePath = ConfigurationReader.FileLocation;   //Getting path value from web.config            
        XmlSerializer serializer = new XmlSerializer(typeof(Devices)); //typeof(object)
                MemoryStream memStream = new MemoryStream();
                serializer.Serialize(memStream, lstDevices);//lstdevices : I take result as a list.
                FileStream file = new FileStream(folderName + "\\Data.xml", FileMode.Create, FileAccess.ReadWrite); //foldername:Specify the path to store the xml file
                memStream.WriteTo(file);
                file.Close();
    

    You can create and store the result as xml file in the desired location.

提交回复
热议问题