Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:15:39+00:00 2026-05-27T18:15:39+00:00

I am a beginner with .NET environment. I have a windows application with three

  • 0

I am a beginner with .NET environment.
I have a windows application with three textboxes and one button. When the user clicks on the button, i want all the textbox values to be serialized in an XML format to a file.
I tried doing it this way,

    DialogResult dr = new DialogResult();
    private void button1_Click(object sender, EventArgs e)
    {
        AddCustomer customer = new AddCustomer();
        customer.textBox1.Text = textBox1.Text;
        customer.textBox2.Text = textBox2.Text;
        customer.textBox3.Text = textBox3.Text;
        customer.textBox4.Text = textBox4.Text;

            saveFileDialog1.InitialDirectory = @"D:";
            saveFileDialog1.Filter = "Xml Files | *.xml";
            if (saveFileDialog1.ShowDialog().Equals(DialogResult.OK))
            {

                SerializeToXML(customer);
            }            
    }

    public void SerializeToXML(AddCustomer customer)
    {

           XmlSerializer serializer = new XmlSerializer(typeof(AddCustomer));
            TextWriter textWriter = new StreamWriter(@"D:\customer.xml");
            serializer.Serialize(textWriter, customer);
            textWriter.Close();
    }

this returned system.invalidoperationexception was unhandled exception

any ideas?
Thanks,
Michael

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:15:40+00:00Added an answer on May 27, 2026 at 6:15 pm

    There are a lot of ways to write XML in .NET. Here is a way using XmlWriter that works for very simple content like in this case:

    string text1 = /* get value of textbox */;
    string text2 = /* get value of textbox */;
    string text3 = /* get value of textbox */;
    
    // Set indent=true so resulting file is more 'human-readable'
    XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
    
    // Put writer in using scope; after end of scope, file is automatically saved.
    using (XmlWriter writer = XmlTextWriter.Create("file.xml", settings))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("root");
        writer.WriteElementString("text1", text1);
        writer.WriteElementString("text2", text2);
        writer.WriteElementString("text3", text3);
        writer.WriteEndElement();
    }
    

    One note: you should avoid doing file operations on the UI thread as this can result in blocking behavior (e.g. the disk can be slow and cause the UI to freeze up while it writes the file, or it could be writing to a network location and hang for a while as it connects). It is best to put up a progress dialog and display a message “Please wait while file is saved…” and do the file operation in the background; a simple way is to post the background operation the thread pool using BeginInvoke/EndInvoke.


    If you want to use the XmlSerializer instead, then you would follow these steps:

    1. Create a public type to act as the root element of your document and mark it with XmlRoot.
    2. Add elements/attributes made of either primitive/built-in types or your own public custom types which are also XML serializable, marking them with XmlElement or XmlAttribute as necessary.
    3. To write the data out, use XmlSerializer.Serialize with an appropriate Stream, StreamWriter, or XmlWriter along with your root object.
    4. To read the data back in, use XmlSerializer.Deseralize with an appropriate Stream, TextReader, or XmlReader, casting the return type back to your root object.

    Full sample.

    The type to serialize:

    [XmlRoot("customer")]
    public class CustomerData
    {
        // Must have a parameterless public constructor
        public CustomerData()
        {
        }
    
        [XmlElement("name")]
        public string Name { get; set; }
    
        [XmlElement("city")]
        public string City { get; set; }
    
        [XmlElement("company")]
        public string Company { get; set; }
    
        public override string ToString()
        {
            return
                "Name=[" + this.Name + "] " +
                "City=[" + this.City + "] " +
                "Company=[" + this.Company + "]";
        }
    }
    

    The code to read/write the data:

    // Initialize the serializer to write and read the data
    XmlSerializer serializer = new XmlSerializer(typeof(CustomerData));
    
    // Initialize the data to serialize
    CustomerData dataToWrite = new CustomerData()
    {
        Name = "Joel Spolsky",
        City = "New York",
        Company = "Fog Creek Software"
    };
    
    // Write it out
    XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
    using (XmlWriter writer = XmlTextWriter.Create("customer.xml", settings))
    {
        serializer.Serialize(writer, dataToWrite);
    }
    
    // Read it back in
    CustomerData dataFromFile = null;
    using (XmlReader reader = XmlTextReader.Create("customer.xml"))
    {
        dataFromFile = (CustomerData)serializer.Deserialize(reader);
    }
    
    Console.WriteLine(dataFromFile);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner.I want to study about Application Domain in ASP.NET. Can anyone explain
This is a beginner level question for asp.net MVC I have the following code
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
I am a beginner to asp.net. I want to sort a gridview but the
I have a beginner level Json question with MVC.net (I've never really used jquery
I am beginner in .NET. One of my firsts task is to change the
I am wanting to learn ASP.Net and am just a beginner. I have done
I am flex beginner. I want to create project in Flex and VS.Net using
I am a beginner of asp.net..I currently have a login page with forgot password
I am a beginner in ASP.Net. We have Validation Controls in ASP.Net. But I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.