Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to save data taken from user in xml file and how to display same data to the user???
Posted

 
Share this answer
 
Comments
sandesh021990 12-Nov-12 5:20am    
i got this article.. but how to retrieve and write into xml file in metro style app...
please help
thanks
I like to use an XML serializable class since it is very easy to maintain and modify. Just add the attributes above the class and properties. Then you can just serialize to an XML and deserialize your XML to an object.

Example:


C#
using System;
using System.IO;
using System.Xml.Serialization;

    [XmlRoot("UserProfile")]
    public class Profile
    {
        [XmlElement()]
        public string Name { get; set; }
        [XmlElement()]
        public int Age { get; set; }
        
        public void Serialize(string uri)
        {
            if (string.IsNullOrEmpty(uri))
                throw new ArgumentNullException("File path invalid.");

            XmlSerializer s = new XmlSerializer(typeof(Profile));
            using (TextWriter w = new StreamWriter(uri))
            {
                s.Serialize(w, this);
                w.Close();
            }
        }

        public static Profile Deserialize(string uri)
        {
            if (string.IsNullOrEmpty(uri))
                throw new ArgumentNullException("File path invalid.");

            using (TextReader r = new StreamReader(uri))
            {
                XmlSerializer s = new XmlSerializer(typeof(Profile));
                return (Profile)s.Deserialize(r);   
            }
        }
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900