Tuesday, June 21, 2016

How to convert class into xml

    In Model :

    [Serializable]
    [XmlRoot(ElementName = "Process")]
    [XmlType("Exweb.Models.ExampleViewModel")]
    public class ExampleModel
    {
        [XmlElement("Organization")]
        public string Organization { get; set; }
        public string  Name { get; set; }
         
        [XmlIgnoreAttribute]
        public RangeCategory<string> Category { get; set; }
    }

In Controller

XmlDocument xmlDoc = new XmlDocument();
            XmlSerializer xmlSerializer = new XmlSerializer(model.GetType());

            using (MemoryStream xmlStream = new MemoryStream())
            {
                xmlSerializer.Serialize(xmlStream, model);
                xmlStream.Position = 0;
                xmlDoc.Load(xmlStream);

                XmlNodeList xmlnode = xmlDoc.GetElementsByTagName("ProcessControl");
                XmlNode root = xmlDoc.DocumentElement;
                

                string path = AppDomain.CurrentDomain.BaseDirectory + @"\Uploads\Process\";   --- save it into folder
                if (!System.IO.File.Exists(path))
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(path);
                    file.Directory.Create();
                }
                xmlDoc.Save(path + model.ProcessName + ".xml");
            }

No comments:

Post a Comment

Comments

Search

Custom Search
Protected by Copyscape Plagiarism Software