3
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by thomas6188
1

Here is an example of code I use to read an XML file.

void binddata()
    {
        DataSet dsName = new DataSet();
        dsName.ReadXml("C:/XMLFILEHERE"));
        foreach (DataColumn da in dsName.Tables[0].Columns)
        {
            da.ColumnMapping = MappingType.Attribute;
        }
     }
0

Please have a look at the code below :

public class Person
    {
        public string Name= "";
        public int age;       
        public void accept()
        {
            Name = Console.ReadLine();
            age= Int16.Parse(Console.ReadLine());
        }
    }
    class Program
    {
        public static void Main()
        {
		//Serialisation--------------
                FileStream fs = new FileStream(@"d:\newfile.xml",FileMode.OpenOrCreate,FileAccess.ReadWrite);
            
                Person obj = new Person();
                obj.accept();

                XmlSerializer ser = new XmlSerializer(obj.GetType());
                StreamWriter wr = new StreamWriter(fs);
                ser.Serialize(wr, obj);
		fs.Close();

		//De-Serialisation--------------
                XmlDocument doc = new XmlDocument();
                doc.Load(@"d:\newfile.xml");
                XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
                XmlSerializer ser1 = new XmlSerializer(obj.GetType());

                object obj1 = ser1.Deserialize(reader);
                Person myObj = (Person)obj1;
                Console.WriteLine(myObj.Name);

        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.