locked
Transfer details from text fields to a new text file through serialization RRS feed

  • Question

  • User-709318330 posted

    I have a web page in developed in ASP.NET with many input fields like radio button,text box,check box. How can i transfer the data to a new file. I have reading regarding XML ,SOAP,Binary serialize methods but not sure how to choose them and which one fits into my case.

    Saturday, March 8, 2014 1:29 AM

Answers

  • User-1454326058 posted

    Hi meka,

    For your requirement, you could have a class to store these input values, then use XML serialization to write the instance to an XML file.

    For example:

     public class Book
        {
            public String title; 
        }
    
    
        public static void WriteXML()
        {
            Book overview = new Book();
            overview.title = "Serialization Overview";
            System.Xml.Serialization.XmlSerializer writer = 
                new System.Xml.Serialization.XmlSerializer(typeof(Book));
    
            System.IO.StreamWriter file = new System.IO.StreamWriter(
                @"c:\temp\SerializationOverview.xml");
            writer.Serialize(file, overview);
            file.Close();
        }
    
    public void ReadXML()
    {
        System.Xml.Serialization.XmlSerializer reader = 
            new System.Xml.Serialization.XmlSerializer(typeof(Book));
        System.IO.StreamReader file = new System.IO.StreamReader(
            @"c:\temp\SerializationOverview.xml");
        Book overview = new Book();
        overview = (Book)reader.Deserialize(file);
    
       string title= overview.title;//Get value
    
    }

    Thanks

    Best Regards  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 10, 2014 2:30 AM

All replies

  • User1508394307 posted

    You can save data to file using

    using (StreamWriter f = new StreamWriter(Server.MapPath("~/data.txt"), true))
    {
       f.WriteLine(TextBox1.Text); // Write value of textbox to the file.
    }      

    Take a look at the documentation http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspx 

    Saturday, March 8, 2014 3:15 AM
  • User-1454326058 posted

    Hi meka,

    For your requirement, you could have a class to store these input values, then use XML serialization to write the instance to an XML file.

    For example:

     public class Book
        {
            public String title; 
        }
    
    
        public static void WriteXML()
        {
            Book overview = new Book();
            overview.title = "Serialization Overview";
            System.Xml.Serialization.XmlSerializer writer = 
                new System.Xml.Serialization.XmlSerializer(typeof(Book));
    
            System.IO.StreamWriter file = new System.IO.StreamWriter(
                @"c:\temp\SerializationOverview.xml");
            writer.Serialize(file, overview);
            file.Close();
        }
    
    public void ReadXML()
    {
        System.Xml.Serialization.XmlSerializer reader = 
            new System.Xml.Serialization.XmlSerializer(typeof(Book));
        System.IO.StreamReader file = new System.IO.StreamReader(
            @"c:\temp\SerializationOverview.xml");
        Book overview = new Book();
        overview = (Book)reader.Deserialize(file);
    
       string title= overview.title;//Get value
    
    }

    Thanks

    Best Regards  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 10, 2014 2:30 AM